summaryrefslogtreecommitdiff
path: root/util/log.h
blob: 9d7df6ec9b4537c15853e5844a6e38387589b107 (plain)
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
// log.h

#pragma once

class Nullstream { 
public:
	Nullstream& operator<<(const char *) { return *this; }
	Nullstream& operator<<(int) { return *this; }
	Nullstream& operator<<(unsigned long) { return *this; }
	Nullstream& operator<<(unsigned) { return *this; }
	Nullstream& operator<<(double) { return *this; }
	Nullstream& operator<<(void *) { return *this; }
	Nullstream& operator<<(long long) { return *this; }
	Nullstream& operator<<(unsigned long long) { return *this; }
	Nullstream& operator<<(const string&) { return *this; }
	Nullstream& operator<< (ostream& ( *endl )(ostream&)) { return *this; }
	Nullstream& operator<< (ios_base& (*hex)(ios_base&)) { return *this; }
};
inline Nullstream& endl ( Nullstream& os ) { }
extern Nullstream nullstream;

#define LOGIT { lock lk(mutex); cout << x; return *this; }
class Logstream {
	static boost::mutex mutex;
public:
	Logstream& operator<<(const char *x) LOGIT
	Logstream& operator<<(char x) LOGIT
	Logstream& operator<<(int x) LOGIT
	Logstream& operator<<(unsigned long x) LOGIT
	Logstream& operator<<(unsigned x) LOGIT
	Logstream& operator<<(double x) LOGIT
	Logstream& operator<<(void *x) LOGIT
	Logstream& operator<<(long long x) LOGIT
	Logstream& operator<<(unsigned long long x) LOGIT
	Logstream& operator<<(const string& x) LOGIT
	Logstream& operator<< (ostream& ( *_endl )(ostream&)) { lock lk(mutex); cout << _endl; return *this; }
	Logstream& operator<< (ios_base& (*_hex)(ios_base&)) { lock lk(mutex); cout << _hex; return *this; }
	Logstream& prolog() {
		lock lk(mutex);
		time_t t;
		time(&t);
		string now(ctime(&t),0,20);
		cout << "~ " << now;
		if( client ) 
			cout << curNs << ' ';
		return *this;
	}
};
inline Logstream& endl ( Logstream& os ) { }
extern Logstream logstream;

// not threadsafe
inline Logstream& problem() { return logstream.prolog(); }

#define cout logstream