-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.cpp
More file actions
91 lines (79 loc) · 1.67 KB
/
logger.cpp
File metadata and controls
91 lines (79 loc) · 1.67 KB
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
#include <cstdio>
#include <cstdarg>
#include <ctime>
#include <chrono>
#include "logger.hpp"
void open_log()
{
// Nothing to do here since I am logging to stdout.
}
void logger ( const std::string & format, ... )
{
va_list ap;
va_start ( ap,format );
vfprintf ( stderr,format.c_str(),ap );
fflush ( stderr );
va_end ( ap );
}
void close_log()
{
// Nothing to do.
}
Log::Log ( const std::string& filename, Log_Level verbosity )
: verbosity ( verbosity )
{
using std::chrono::system_clock;
logFile.open ( filename.c_str(), std::ios::app | std::ios::out);
time_t tt = system_clock::to_time_t(system_clock::now());
logFile << "Opened at: " << ctime(&tt) <<std::endl;
}
Log::~Log()
{
using std::chrono::system_clock;
time_t tt = system_clock::to_time_t(system_clock::now());
logFile << "Closed at: " << ctime(&tt);
logFile.flush();
logFile.close();
}
void Log::operator() ( const std::string& message, const Log_Level priority ) {
if ( priority <= verbosity ) {
logFile << message << std::endl;
logFile.flush();
}
}
Log& Log::operator<< ( const std::string& message )
{
if ( current_priority <= verbosity )
{
logFile << message;
logFile.flush();
}
return *this;
}
Log& Log::operator<< ( const char message[] )
{
if ( current_priority <= verbosity )
{
logFile << message;
logFile.flush();
}
return *this;
}
Log& Log::operator<< ( const long long int message )
{
if ( current_priority <= verbosity )
{
logFile << message;
logFile.flush();
}
return *this;
}
Log& Log::operator<< ( const Log_Level new_priority )
{
current_priority = new_priority;
return *this;
}
Log_Level Log::get_priority()
{
return current_priority;
}