File: syslogloglocation.cpp

package info (click to toggle)
crtmpserver 1.0~dfsg-5.3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,424 kB
  • sloc: cpp: 56,355; sh: 411; makefile: 21
file content (140 lines) | stat: -rw-r--r-- 3,951 bytes parent folder | download | duplicates (2)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* 
 *  Copyright (c) 2010,
 *  Gavriloaie Eugen-Andrei (shiretu@gmail.com)
 *  
 *  This file is part of crtmpserver.
 *  crtmpserver 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 3 of the License, or
 *  (at your option) any later version.
 *  
 *  crtmpserver 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 crtmpserver.  If not, see <http://www.gnu.org/licenses/>.
 */


#ifdef HAS_SYSLOG
#include "utils/logging/syslogloglocation.h"
#include "utils/logging/formatter.h"
#include "common.h"
#include <syslog.h>

SyslogLogLocation::SyslogLogLocation(Variant &configuration, string identifier, bool appendSourceFileLine, int32_t specificLevel)
: BaseLogLocation(configuration) {
	_appendSourceFileLine = appendSourceFileLine;
	_identifier = identifier;
	_priorities[_FINEST_] = LOG_DEBUG;
	_priorities[_FINE_] = LOG_DEBUG;
	_priorities[_DEBUG_] = LOG_DEBUG;
	_priorities[_INFO_] = LOG_INFO;
	_priorities[_WARNING_] = LOG_WARNING;
	_priorities[_ERROR_] = LOG_ERR;
	_priorities[_FATAL_] = LOG_ERR;
	_priorities[_PROD_ACCESS_] = LOG_ERR;
	_priorities[_PROD_ERROR_] = LOG_ERR;
	_specificLevel = specificLevel;
	_enforceLoggerName = (_configuration[CONF_LOG_APPENDER_NAME] != (string) "");
	_pDefualtFormatter = NULL;
	InitFormatters();
}

SyslogLogLocation::~SyslogLogLocation() {
	if (_pDefualtFormatter != NULL) {
		delete _pDefualtFormatter;
		_pDefualtFormatter = NULL;
	}

	FOR_MAP(_formatters, string, Formatter *, i) {
		delete MAP_VAL(i);
	}
	_formatters.clear();
}

void SyslogLogLocation::Log(int32_t level, string fileName, uint32_t lineNumber,
		string functionName, string message) {
	return;
}

void SyslogLogLocation::Log(int32_t level, string fileName, uint32_t lineNumber,
		string functionName, Variant &le) {
	if (_specificLevel != 0) {
		if (_specificLevel != level) {
			return;
		}
	} else {
		if (_level < 0 || level > _level) {
			return;
		}
	}
	if (_enforceLoggerName) {
		if (_configuration[CONF_LOG_APPENDER_NAME] != le["loggerName"]) {
			return;
		}
	}

	string finalMessage = ComputeMessage(le);
	if (finalMessage == "")
		return;

	int priority = (MAP_HAS1(_priorities, level) ? _priorities[level] : LOG_DEBUG) | LOG_USER;

	if (_appendSourceFileLine) {
		syslog(priority, "%s %s:%" PRIu32":%s %s",
				STR(le["loggerName"]),
				STR(fileName),
				lineNumber,
				STR(functionName),
				STR(finalMessage));
	} else {
		syslog(priority, "%s %s",
				STR(le["loggerName"]),
				STR(finalMessage));
	}
}

void SyslogLogLocation::InitFormatters() {
	if (!_configuration.HasKeyChain(V_MAP, false, 1, CONF_LOG_APPENDER_FORMAT))
		return;

	string defaultFormatter = "";
	if (_configuration[CONF_LOG_APPENDER_FORMAT][(uint32_t) 1] == V_STRING) {
		defaultFormatter = (string) _configuration[CONF_LOG_APPENDER_FORMAT][(uint32_t) 1];
	}
	_configuration[CONF_LOG_APPENDER_FORMAT].RemoveAt(1);
	if (defaultFormatter != "") {
		_pDefualtFormatter = Formatter::GetInstance(defaultFormatter);
	}

	FOR_MAP(_configuration[CONF_LOG_APPENDER_FORMAT], string, Variant, i) {
		if ((MAP_VAL(i) != V_STRING)
				|| ((string) MAP_VAL(i) == ""))
			continue;
		Formatter *pTemp = Formatter::GetInstance(MAP_VAL(i));
		_formatters[MAP_KEY(i)] = pTemp;
	}
}

string SyslogLogLocation::ComputeMessage(Variant &le) {
	Formatter *pFormatter = NULL;
	if (MAP_HAS1(_formatters, (string) le["operation"]))
		pFormatter = _formatters[(string) le["operation"]];
	else
		pFormatter = _pDefualtFormatter;

	if (pFormatter == NULL)
		return "";

	return pFormatter->Format(le);
}

void SyslogLogLocation::SignalFork() {

}

#endif /* HAS_SYSLOG */