This repository has been archived by the owner on Jan 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Log.cpp
97 lines (84 loc) · 2.11 KB
/
Log.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* Logger.cpp
*
* Created on: 01.08.2012
* Author: Forsaken
*/
#include "Log.h"
#include "logging/ConsoleTarget.h"
#include <time.h>
#include <stdio.h>
#include <iostream>
#include <iomanip>
using namespace std;
using namespace Logging;
LogTarget* Log::logTarget = NULL;
Log::Log(const char* name) : sName(name) {
iLevel = 0;
iSections = LogSections::SECTION_ANY;
bWritename = true;
}
Log::Log(const char* name, int bitSections) : sName(name) {
iLevel = 0;
iSections = bitSections;
bWritename = true;
}
Log::~Log() {
// TODO Auto-generated destructor stub
}
Log& Log::get(const int level) {
iLevel = level;
return *this;
}
void Log::writeName() {
bWritename = false;
// Output timestamp
time_t now;
time(&now);
struct tm *nowLocal = localtime ( &now );
Log::getTarget() << dec << setfill('0') << "[" << setw(2) << nowLocal->tm_hour << ":" << setw(2) << nowLocal->tm_min << ":" << setw(2) << nowLocal->tm_sec << "]" << " ";
// Output log level and category
switch (iLevel) {
case LogLevel::FATAL:
Log::getTarget() << "[FATAL]\t";
break;
case LogLevel::ERR0R:
Log::getTarget() << "[ERROR]\t";
break;
case LogLevel::WARNING:
Log::getTarget() << "[WARN]\t";
break;
case LogLevel::INFO:
Log::getTarget() << "[INFO]\t";
break;
case LogLevel::DEBUG:
Log::getTarget() << "[DEBUG]\t";
break;
default:
Log::getTarget() << "[UNDEF]\t";
break;
}
Log::getTarget() << sName << ": ";
}
void Log::writeBinaryAsHex(const int level, const void* buffer, unsigned int bufferLength) {
char sBuffer[2];
Log& logTarget = this->get(level);
for (ULONG i = 0; i < bufferLength; i++) {
sprintf(sBuffer, "%02x", *((PUCHAR)buffer + i));
logTarget << sBuffer;
}
}
void Log::addTarget(const string &name, Target* target) {
getTarget().addTarget(name, target);
}
LogTarget& Log::getTarget() {
if (logTarget == NULL) {
logTarget = new LogTarget(LogSections::SECTION_ANY);
// TODO: Dynamic log level
logTarget->addTarget("console", new ConsoleTarget(LogLevel::DEBUG, LogSections::SECTION_ANY));
}
return *logTarget;
}
Target* Log::getTarget(const string &name) {
return getTarget().getTarget(name);
}