1 /*!\file rlog.cxx
2
3 \brief RLog logging routines
4
5 *//*
6
7 ClamFS - An user-space anti-virus protected file system
8 Copyright (C) 2007-2019 Krzysztof Burghardt
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25 #include <cstdlib>
26
27 #include "rlog.hxx"
28
29 #include <unistd.h>
30
31 namespace clamfs {
32
33 extern config_t config;
34
35 /*!\brief Debug channel (debugging messages goes here) */
36 RLogChannel *Debug = DEF_CHANNEL("debug", Log_Debug);
37 /*!\brief Information channel (most messages goes here) */
38 RLogChannel *Info = DEF_CHANNEL("info", Log_Info);
39 /*!\brief Warning channel (warnings goes here) */
40 RLogChannel *Warn = DEF_CHANNEL("warn", Log_Warning);
41
42 /*!\brief Stdout logging node */
43 static StdioNode *stdLog = NULL;
44 /*!\brief Syslog logging node */
45 static SyslogNode *logNode = NULL;
46 /*!\brief Log file descriptor */
47 static int fileLog = 0;
48 /*!\brief Log file logging node */
49 static StdioNode *fileLogNode = NULL;
50
51 /*!\brief Opens stdio logging target
52
53 This function opens stdio logging target
54 and subscribes all RLog channels for it.
55 */
RLogOpenStdio()56 void RLogOpenStdio() {
57 #ifndef NDEBUG
58 stdLog = new StdioNode(STDOUT_FILENO, StdioNode::OutputContext |
59 StdioNode::OutputThreadId | StdioNode::OutputColor);
60 #else
61 stdLog = new StdioNode(STDOUT_FILENO);
62 #endif
63 stdLog->subscribeTo( RLOG_CHANNEL("") );
64 DEBUG("initial log attached to stdio");
65 }
66
67 /*!\brief Closes stdio logging target
68 */
RLogCloseStdio()69 void RLogCloseStdio() {
70 DEBUG("will close initial stdio log");
71 delete stdLog;
72 stdLog = NULL;
73 DEBUG("closed initial stdio log");
74 }
75
76 /*!\brief Opens syslog logging target
77
78 This function opens syslog logging target
79 and subscribes all RLog channels for it.
80 */
RLogOpenSyslog()81 void RLogOpenSyslog() {
82 logNode = new SyslogNode("clamfs");
83 logNode->subscribeTo( RLOG_CHANNEL("") );
84 rLog(Info, "logs goes to syslog");
85 }
86
87 /*!\brief Opens log file logging target
88 \param filename log file name
89
90 This function opens log file logging target
91 and subscribes all RLog channels for it.
92 */
RLogOpenLogFile(const char * filename)93 void RLogOpenLogFile(const char *filename) {
94 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
95
96 fileLog = open(filename, O_WRONLY | O_CREAT | O_APPEND, mode);
97 if (fileLog > 0) { /* file open successful */
98 #ifndef NDEBUG
99 fileLogNode = new StdioNode(fileLog, StdioNode::OutputContext |
100 StdioNode::OutputThreadId);
101 #else
102 fileLogNode = new StdioNode(fileLog);
103 #endif
104 fileLogNode->subscribeTo( RLOG_CHANNEL("") );
105 rLog(Info, "log goes to file %s", filename);
106 } else { /* file open failed */
107 rLog(Warn, "cannot open log file %s", filename);
108 exit(EXIT_FAILURE);
109 }
110 }
111
112 /*!\brief Closes log file logging target
113 */
RLogCloseLogFile()114 void RLogCloseLogFile() {
115 if (fileLog > 0) { /* file open, close it */
116 delete fileLogNode;
117 fileLogNode = NULL;
118 close(fileLog);
119 }
120 }
121
122 } /* namespace clamfs */
123
124 /* EoF */
125