Browse Source

moved log file from daemon to log

pull/72/head
orignal 11 years ago
parent
commit
2062305f88
  1. 7
      Daemon.cpp
  2. 4
      Daemon.h
  3. 23
      Log.cpp
  4. 14
      Log.h

7
Daemon.cpp

@ -63,12 +63,7 @@ namespace i2p
#else #else
logfile_path.append("\\debug.log"); logfile_path.append("\\debug.log");
#endif #endif
logfile.open(logfile_path, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc); g_Log.SetLogFile (logfile_path);
if (!logfile.is_open())
exit(-17);
LogPrint("Logging to file enabled.");
LogPrint("CMD parameters:"); LogPrint("CMD parameters:");
for (int i = 0; i < argc; ++i) for (int i = 0; i < argc; ++i)

4
Daemon.h

@ -1,5 +1,5 @@
#pragma once #pragma once
#include <fstream> #include <string>
#ifdef _WIN32 #ifdef _WIN32
#define Daemon i2p::util::DaemonWin32::Instance() #define Daemon i2p::util::DaemonWin32::Instance()
@ -24,8 +24,6 @@ namespace i2p
int running; int running;
std::ofstream logfile;
protected: protected:
Daemon_Singleton(); Daemon_Singleton();
virtual ~Daemon_Singleton(); virtual ~Daemon_Singleton();

23
Log.cpp

@ -1,20 +1,29 @@
#include "Log.h" #include "Log.h"
#include "Daemon.h"
Log g_Log; Log g_Log;
void LogMsg::Process() void LogMsg::Process()
{ {
if (Daemon.isLogging == 1 && Daemon.logfile.is_open())
Daemon.logfile << s.str();
output << s.str(); output << s.str();
std::cout << s.str (); // TODO: delete later
} }
void Log::Flush () void Log::Flush ()
{ {
if (Daemon.isLogging == 1 && Daemon.logfile.is_open()) if (m_LogFile)
Daemon.logfile.flush(); 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
Log.h

@ -1,8 +1,10 @@
#ifndef LOG_H__ #ifndef LOG_H__
#define LOG_H__ #define LOG_H__
#include <string>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <fstream>
#include <functional> #include <functional>
#include "Queue.h" #include "Queue.h"
@ -20,11 +22,19 @@ class Log: public i2p::util::MsgQueue<LogMsg>
{ {
public: 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: private:
void Flush (); void Flush ();
private:
std::ofstream * m_LogFile;
}; };
extern Log g_Log; extern Log g_Log;
@ -45,7 +55,7 @@ void LogPrint (std::stringstream& s, TValue arg, TArgs... args)
template<typename... TArgs> template<typename... TArgs>
void LogPrint (TArgs... args) void LogPrint (TArgs... args)
{ {
LogMsg * msg = new LogMsg (); LogMsg * msg = g_Log.GetLogFile () ? new LogMsg (*g_Log.GetLogFile ()) : new LogMsg ();
LogPrint (msg->s, args...); LogPrint (msg->s, args...);
msg->s << std::endl; msg->s << std::endl;
g_Log.Put (msg); g_Log.Put (msg);

Loading…
Cancel
Save