Skip to content

Commit

Permalink
moved log file from daemon to log
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Apr 24, 2014
1 parent 3e81123 commit 2062305
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
7 changes: 1 addition & 6 deletions Daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,7 @@ namespace i2p
#else
logfile_path.append("\\debug.log");
#endif
logfile.open(logfile_path, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);

if (!logfile.is_open())
exit(-17);

LogPrint("Logging to file enabled.");
g_Log.SetLogFile (logfile_path);

LogPrint("CMD parameters:");
for (int i = 0; i < argc; ++i)
Expand Down
4 changes: 1 addition & 3 deletions Daemon.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include <fstream>
#include <string>

#ifdef _WIN32
#define Daemon i2p::util::DaemonWin32::Instance()
Expand All @@ -24,8 +24,6 @@ namespace i2p

int running;

std::ofstream logfile;

protected:
Daemon_Singleton();
virtual ~Daemon_Singleton();
Expand Down
23 changes: 16 additions & 7 deletions Log.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
#include "Log.h"

#include "Daemon.h"

Log g_Log;

void LogMsg::Process()
{
if (Daemon.isLogging == 1 && Daemon.logfile.is_open())
Daemon.logfile << s.str();

output << s.str();

std::cout << s.str (); // TODO: delete later
}

void Log::Flush ()
{
if (Daemon.isLogging == 1 && Daemon.logfile.is_open())
Daemon.logfile.flush();
if (m_LogFile)
m_LogFile->flush();
}

void Log::SetLogFile (const std::string& fullFilePath)
{
if (m_LogFile) delete m_LogFile;
m_LogFile = new std::ofstream (fullFilePath, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
if (m_LogFile->is_open ())
LogPrint("Logging to file ", fullFilePath, " enabled.");
else
{
delete m_LogFile;
m_LogFile = nullptr;
}
}
14 changes: 12 additions & 2 deletions Log.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#ifndef LOG_H__
#define LOG_H__

#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <functional>
#include "Queue.h"

Expand All @@ -20,11 +22,19 @@ class Log: public i2p::util::MsgQueue<LogMsg>
{
public:

Log () { SetOnEmpty (std::bind (&Log::Flush, this)); };
Log (): m_LogFile (nullptr) { SetOnEmpty (std::bind (&Log::Flush, this)); };
~Log () { delete m_LogFile; };

void SetLogFile (const std::string& fullFilePath);
std::ofstream * GetLogFile () const { return m_LogFile; };

private:

void Flush ();

private:

std::ofstream * m_LogFile;
};

extern Log g_Log;
Expand All @@ -45,7 +55,7 @@ void LogPrint (std::stringstream& s, TValue arg, TArgs... args)
template<typename... TArgs>
void LogPrint (TArgs... args)
{
LogMsg * msg = new LogMsg ();
LogMsg * msg = g_Log.GetLogFile () ? new LogMsg (*g_Log.GetLogFile ()) : new LogMsg ();
LogPrint (msg->s, args...);
msg->s << std::endl;
g_Log.Put (msg);
Expand Down

0 comments on commit 2062305

Please sign in to comment.