2011-08-19 18:59:21 +00:00
|
|
|
#include "Log.h"
|
|
|
|
|
2011-08-21 21:56:33 +00:00
|
|
|
#include "Platform.h"
|
|
|
|
#include "StringUtils.h"
|
2011-08-19 18:59:21 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <iostream>
|
|
|
|
|
2011-08-21 21:56:33 +00:00
|
|
|
#ifdef PLATFORM_UNIX
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2011-08-19 18:59:21 +00:00
|
|
|
Log m_globalLog;
|
|
|
|
|
2011-08-21 21:56:33 +00:00
|
|
|
#ifdef PLATFORM_UNIX
|
|
|
|
pid_t currentProcessId = 0;
|
|
|
|
pid_t processId()
|
|
|
|
{
|
|
|
|
if (currentProcessId == 0)
|
|
|
|
{
|
|
|
|
currentProcessId = getpid();
|
|
|
|
}
|
|
|
|
return currentProcessId;
|
|
|
|
}
|
2011-08-22 14:50:39 +00:00
|
|
|
#else
|
|
|
|
DWORD currentProcessId = 0;
|
|
|
|
DWORD processId()
|
|
|
|
{
|
|
|
|
if (currentProcessId == 0)
|
|
|
|
{
|
|
|
|
currentProcessId = GetCurrentProcessId();
|
|
|
|
}
|
|
|
|
return currentProcessId;
|
|
|
|
}
|
2011-08-21 21:56:33 +00:00
|
|
|
#endif
|
|
|
|
|
2011-08-19 18:59:21 +00:00
|
|
|
Log* Log::instance()
|
|
|
|
{
|
|
|
|
return &m_globalLog;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::Log()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::~Log()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Log::open(const std::string& path)
|
|
|
|
{
|
2011-08-21 21:56:33 +00:00
|
|
|
m_output.open(path.c_str());
|
2011-08-19 18:59:21 +00:00
|
|
|
}
|
|
|
|
|
2011-08-21 21:56:33 +00:00
|
|
|
void Log::writeToStream(std::ostream& stream, Type type, const char* text)
|
2011-08-19 18:59:21 +00:00
|
|
|
{
|
2011-08-21 20:47:33 +00:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case Info:
|
2011-08-21 21:56:33 +00:00
|
|
|
stream << "INFO ";
|
2011-08-21 20:47:33 +00:00
|
|
|
break;
|
|
|
|
case Warn:
|
2011-08-21 21:56:33 +00:00
|
|
|
stream << "WARN ";
|
2011-08-21 20:47:33 +00:00
|
|
|
break;
|
|
|
|
case Error:
|
2011-08-21 21:56:33 +00:00
|
|
|
stream << "ERROR ";
|
2011-08-21 20:47:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-08-21 21:56:33 +00:00
|
|
|
stream << '(' << intToStr(processId()) << ") " << text << std::endl;
|
|
|
|
}
|
2011-08-21 20:47:33 +00:00
|
|
|
|
2011-08-21 21:56:33 +00:00
|
|
|
void Log::write(Type type, const char* text)
|
|
|
|
{
|
|
|
|
writeToStream(std::cerr,type,text);
|
|
|
|
if (m_output.is_open())
|
2011-08-21 20:47:33 +00:00
|
|
|
{
|
2011-08-21 21:56:33 +00:00
|
|
|
writeToStream(m_output,type,text);
|
2011-08-21 20:47:33 +00:00
|
|
|
}
|
2011-08-19 18:59:21 +00:00
|
|
|
}
|
|
|
|
|
2011-08-23 22:54:37 +00:00
|
|
|
std::string Log::defaultPath()
|
|
|
|
{
|
|
|
|
std::string path = "update-log.txt";
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|