GridComputing
Job Management in Grid Computing
Main Page
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
log.h
Go to the documentation of this file.
1
#ifndef LOG_H_
2
#define LOG_H_
3
4
#include <string>
5
#include <fstream>
6
#include <mutex>
7
#include <iostream>
8
#include <cstdarg>
9
#include <sstream>
10
12
struct
Writer
13
{
14
virtual
void
Write
(
const
std::string& val) = 0;
15
};
16
18
struct
ConsoleWritePolicy
:
public
Writer
19
{
20
void
Write
(
const
std::string& val) { std::cout <<
'\r'
<< val << std::endl; }
21
};
22
24
struct
FileWritePolicy
:
public
Writer
25
{
26
FileWritePolicy
() {
_file
.open(
"log.txt"
); }
27
28
~FileWritePolicy
() {
_file
.close(); }
29
30
void
Write
(
const
std::string& val) {
_file
<< val << std::endl; }
31
32
std::ofstream
_file
;
33
};
34
36
39
template
<
class
WritePolicy>
40
class
Logger
41
{
42
public
:
44
static
Logger
*
Instance
()
45
{
46
if
(!
_instance
)
47
_instance
=
new
Logger<WritePolicy>
;
48
return
_instance
;
49
}
50
52
void
Log
(
const
char
* format, ...)
53
{
54
char
buffer[512];
55
va_list args;
56
va_start(args, format);
57
vsprintf(buffer, format, args);
58
va_end(args);
59
60
std::ostringstream out;
61
62
_mutex
.lock();
63
64
time_t now = time(0);
65
tm* current = localtime(&now);
66
67
out <<
'['
<< current->tm_hour <<
':'
<< current->tm_min <<
':'
<< current->tm_sec <<
']'
<<
": "
<< buffer;
68
_writer
.Write(out.str());
69
70
_mutex
.unlock();
71
}
72
73
private
:
74
static
Logger
*
_instance
;
75
std::mutex
_mutex
;
76
WritePolicy
_writer
;
77
};
78
79
template
<
class
WritePolicy>
80
Logger<WritePolicy>
*
Logger<WritePolicy>::_instance
= NULL;
81
83
#define Console ConsoleWritePolicy
84
86
#define FileWP FileWritePolicy
87
89
#define sLog(policy) Logger<policy>::Instance()
90
91
#endif // LOG_H_
src
log.h
Generated on Sun Dec 30 2012 14:13:37 for GridComputing by
1.8.2