*
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
#include "CLog.h"
|
||||
|
||||
namespace LifeCore
|
||||
{
|
||||
CLog::CLog()
|
||||
:logToConsole(true)
|
||||
{
|
||||
hLogFile = CreateFile(String(_text("engine_log.txt")).c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
SetFilePointer(hLogFile,0,NULL, FILE_END);
|
||||
Write(String(_text("\nLogging started!")).c_str());
|
||||
}
|
||||
void CLog::LogToConsole(bool condition)
|
||||
{
|
||||
logToConsole = condition;
|
||||
}
|
||||
CLog::~CLog()
|
||||
{
|
||||
if(hLogFile)
|
||||
{
|
||||
Write(_text("Closing log"));
|
||||
CloseHandle(hLogFile);
|
||||
}
|
||||
}
|
||||
void CLog::Write(const String& message)
|
||||
{
|
||||
DWORD nb = 0;
|
||||
WriteFile(hLogFile, message.c_str(), string_size(message), &nb, NULL);
|
||||
WriteFile(hLogFile, L"\n", 1, &nb, NULL);
|
||||
|
||||
if(logToConsole)
|
||||
printf("Log: %s\n", message.c_str());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef CLOG_H__
|
||||
#define CLOG_H__
|
||||
|
||||
#include "../Singleton.h"
|
||||
#include "../LString.h"
|
||||
#include <windows.h>
|
||||
|
||||
namespace LifeCore
|
||||
{
|
||||
#define LOG_MESSAGE(message) LifeCore::CLog::me()->Write(STR(##message##))
|
||||
#define LOG_ERROR(message, module, method) LifeCore::CLog::me()->Write("Error occured in method: " + STR(##module##) + "::" + STR(##method##) + "\n" + STR(##message##))
|
||||
|
||||
class CLog;
|
||||
|
||||
class CLog : public Singleton<CLog>
|
||||
{
|
||||
public:
|
||||
CLog();
|
||||
~CLog();
|
||||
void Write(const String& message);
|
||||
void LogToConsole(bool condition);
|
||||
private:
|
||||
HANDLE hLogFile;
|
||||
bool logToConsole;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,94 @@
|
||||
#include "NLog.h"
|
||||
//#include <io.h>
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <Windows.h>
|
||||
#include <tchar.h>
|
||||
|
||||
namespace LifeCore {
|
||||
NettleLog::NettleLog(LOG_LEVEL _level)
|
||||
: AbortOnError(true), bOutConsole(true), bOutFile(false), bOutInterface(false), level(_level) {
|
||||
//TODO: remove allocation of stringstream
|
||||
stm = new std::stringstream();
|
||||
}
|
||||
NettleLog::~NettleLog() {
|
||||
//std::stringstream stm;
|
||||
*stm << "\n";
|
||||
/*stm->seekg (0, std::ios::end);
|
||||
|
||||
size_t length = (size_t)stm->tellg();
|
||||
stm->seekg (0, std::ios::beg);
|
||||
|
||||
char* buffer = new char [length + 1];
|
||||
buffer[length] = 0;
|
||||
stm->read (buffer,length);*/
|
||||
//_write(1, buffer, length);
|
||||
std::string str = stm->str();
|
||||
std::cout << str;
|
||||
|
||||
#if defined(NOS_WINDOWS) && defined(_DEBUG)
|
||||
OutputDebugString(str.c_str());
|
||||
#endif
|
||||
|
||||
delete stm;
|
||||
// delete[] buffer;
|
||||
}
|
||||
NettleLog& NettleLog::operator <<(bool val) {
|
||||
*stm << (val ? "true" : "false");
|
||||
return *this;
|
||||
}
|
||||
NettleLog& NettleLog::operator <<(uint32 val) {
|
||||
*stm << val;
|
||||
return *this;
|
||||
}
|
||||
NettleLog& NettleLog::operator <<(long val) {
|
||||
*stm << val;
|
||||
return *this;
|
||||
}
|
||||
NettleLog& NettleLog::operator <<(unsigned long val) {
|
||||
*stm << val;
|
||||
return *this;
|
||||
}
|
||||
NettleLog& NettleLog::operator <<(uint16 val) {
|
||||
*stm << val;
|
||||
return *this;
|
||||
}
|
||||
NettleLog& NettleLog::operator <<(uint64 val) {
|
||||
*stm << val;
|
||||
return *this;
|
||||
}
|
||||
NettleLog& NettleLog::operator <<(int64 val) {
|
||||
*stm << val;
|
||||
return *this;
|
||||
}
|
||||
NettleLog& NettleLog::operator <<(int32 val) {
|
||||
*stm << val;
|
||||
return *this;
|
||||
}
|
||||
NettleLog& NettleLog::operator <<(int16 val) {
|
||||
*stm << val;
|
||||
return *this;
|
||||
}
|
||||
NettleLog& NettleLog::operator <<(int8 val) {
|
||||
*stm << val;
|
||||
return *this;
|
||||
}
|
||||
NettleLog& NettleLog::operator <<(float val) {
|
||||
*stm << val;
|
||||
return *this;
|
||||
}
|
||||
NettleLog& NettleLog::operator <<(double val) {
|
||||
*stm << val;
|
||||
return *this;
|
||||
}
|
||||
NettleLog& NettleLog::operator <<(const String& val) {
|
||||
*stm << val.c_str();
|
||||
return *this;
|
||||
}
|
||||
NettleLog& NettleLog::operator <<(const char* val) {
|
||||
*stm << val;
|
||||
return *this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
#ifndef NLog_h__
|
||||
#define NLog_h__
|
||||
|
||||
#include <sstream>
|
||||
#include <ostream>
|
||||
#include <strstream>
|
||||
#include <iostream>
|
||||
#include "../Types.h"
|
||||
|
||||
namespace LifeCore {
|
||||
enum LOG_LEVEL {
|
||||
LOG_WARNING,
|
||||
LOG_ERROR,
|
||||
LOG_INFO
|
||||
};
|
||||
|
||||
class ILogOutput {
|
||||
public:
|
||||
virtual bool Write(LOG_LEVEL level, const String& message) = 0;
|
||||
};
|
||||
|
||||
class NettleLog {
|
||||
public:
|
||||
std::stringstream* stm;
|
||||
//std::stringstream stm;
|
||||
NettleLog(LOG_LEVEL _level);
|
||||
~NettleLog();
|
||||
NettleLog& operator <<(int8 val);
|
||||
NettleLog& operator <<(int16 val);
|
||||
NettleLog& operator <<(uint16 val);
|
||||
NettleLog& operator <<(int32 val);
|
||||
NettleLog& operator <<(uint32 val);
|
||||
NettleLog& operator <<(uint64 val);
|
||||
NettleLog& operator <<(int64 val);
|
||||
NettleLog& operator <<(long val);
|
||||
NettleLog& operator <<(unsigned long val);
|
||||
NettleLog& operator <<(float val);
|
||||
NettleLog& operator <<(double val);
|
||||
NettleLog& operator <<(const String& val);
|
||||
NettleLog& operator <<(const char* val);
|
||||
NettleLog& operator <<(bool val);
|
||||
private:
|
||||
LOG_LEVEL level;
|
||||
|
||||
bool AbortOnError;
|
||||
bool bOutConsole;
|
||||
bool bOutFile;
|
||||
bool bOutInterface;
|
||||
};
|
||||
|
||||
inline NettleLog LogWarning() { return NettleLog(LOG_WARNING); }
|
||||
inline NettleLog LogError() { return NettleLog(LOG_ERROR); }
|
||||
inline NettleLog LogInfo() { return NettleLog(LOG_INFO); }
|
||||
}
|
||||
#endif // NLog_h__
|
||||
Reference in New Issue
Block a user