summaryrefslogtreecommitdiffstats
path: root/src/log.h
blob: 5e5fc67f267a56a62de11f55e792feb45ce0ab4a (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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
    Copyright (C) 2005-2009  Michel de Boer <michel@twinklephone.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef _LOG_H
#define _LOG_H

#include <string>
#include <fstream>
#include "threads/mutex.h"
#include "threads/sema.h"
#include "threads/thread.h"

using namespace std;

#define LOG_FILENAME    "twinkle.log"

// Severity of a log message
enum t_log_severity {
	LOG_INFO,
	LOG_WARNING,
	LOG_CRITICAL,
	LOG_DEBUG
};

// Message class
enum t_log_class {
	LOG_NORMAL,
	LOG_SIP,
	LOG_STUN,
	LOG_MEMORY
};

class t_log {
private:
	/** Maximum length of a logged string (bytes) */
	static const string::size_type MAX_LEN_LOG_STRING = 1024;

        string          log_filename;
        ofstream        *log_stream;

	// Mutex for exclusive acces to the log file
	t_mutex		mtx_log;

	// Indicates if logging is disabled
	bool		log_disabled;
	bool		log_report_disabled;
	
	// Indicates if the user should be informed about log updates
	bool		inform_user;
	
	// Indicates if new data for the log viewer is available
	t_semaphore	*sema_logview;
	
	// Thread for updating the log viewer
	t_thread	*thr_logview;

        // Move the current log file to the .old log file
        bool move_current_to_old(void);

public:
        t_log();
        ~t_log();

        // Write a report with header and footer
        void write_report(const string &report, const string &func_name); // normal, info
	void write_report(const string &report, const string &func_name,
		t_log_class log_class, t_log_severity severity = LOG_INFO);

        // Write header
	// This locks the mtx_log. So you must call write footer to release
	// the log again!
        void write_header(const string &func_name); // class normal, severity info
	void write_header(const string &func_name, t_log_class log_class,
	                  t_log_severity severity = LOG_INFO);

        // Write footer
	// This unlocks the mtx_log.
        void write_footer(void);

        // Write raw data
        void write_raw(const string &raw);
        void write_raw(int raw);
        void write_raw(unsigned int raw);
        void write_raw(unsigned short raw);
        void write_raw(unsigned long raw);
	void write_raw(long raw);
	void write_bool(bool raw);

        // Write end of line
        void write_endl(void);
        
        // Return the full path name of the log file
        string get_filename(void) const;
        
        // Enable/disable user informs on updates
        void enable_inform_user(bool on);
        
        // Block till log information is available for log viewer
        void wait_for_log(void);
};

extern t_log *log_file;

#endif