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 @@ -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)

4
Daemon.h

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

23
Log.cpp

@ -1,20 +1,29 @@ @@ -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
Log.h

@ -1,8 +1,10 @@ @@ -1,8 +1,10 @@
#ifndef LOG_H__
#define LOG_H__
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <functional>
#include "Queue.h"
@ -20,11 +22,19 @@ class Log: public i2p::util::MsgQueue<LogMsg> @@ -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;
@ -45,7 +55,7 @@ void LogPrint (std::stringstream& s, TValue arg, TArgs... args) @@ -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);

Loading…
Cancel
Save