1 /*
2  *  log.cpp -- Log handler
3  *
4  *  Copyright (C) 2004 Tony Sin(x) '76 <administrator@tortugalabs.it>
5  *  All rights reserved.
6  *
7  */
8 
9 /*
10  *			 GNU GENERAL PUBLIC LICENSE
11  *			    Version 2, June 1991
12  *
13  *  Copyright (C) 1989, 1991 Free Software Foundation, Inc.
14  *                           675 Mass Ave, Cambridge, MA 02139, USA
15  *  Everyone is permitted to copy and distribute verbatim copies
16  *  of this license document, but changing it is not allowed.
17  *
18  *  This program is free software; you can redistribute it and/or modify
19  *  it under the terms of the GNU General Public License as published by
20  *  the Free Software Foundation; either version 2 of the License, or
21  *  (at your option) any later version.
22 
23  *  This program is distributed in the hope that it will be useful,
24  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
25  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  *  GNU General Public License for more details.
27 
28  *  You should have received a copy of the GNU General Public License
29  *  along with this program; if not, write to the Free Software
30  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
31  *
32  */
33 
34 #ifdef HAVE_CONFIG_H
35   #ifndef __main_config_h__
36     #define __main_config_h__
37     #include "../config.h"
38   #endif
39 
40   #ifdef HAVE_STDIO_H
41     #include <stdio.h>
42   #endif
43 
44   #ifdef HAVE_SYSLOG_H
45     #include <syslog.h>
46   #endif
47 
48   #ifdef HAVE_STRING_H
49     #include <string.h>
50   #endif
51 
52   #ifdef TIME_WITH_SYS_TIME
53     #include <sys/time.h>
54     #include <time.h>
55   #else
56     #ifdef HAVE_SYS_TIME_H
57       #include <sys/time.h>
58     #else
59       #include <time.h>
60     #endif
61   #endif
62 #endif
63 
64 #include "log.h"
65 
cLog()66 cLog::cLog()
67 {
68   LogSelected = _none;
69 }
70 
cLog(cConfig * object)71 cLog::cLog(cConfig *object)
72 {
73   int_buf = new char[INTERNAL_BUF_SIZE];
74 
75   switch (LogSelected = ((log_type) atoi(object->GetValue(_log))))
76   {
77     case _none: break;
78     case _system: openlog("Icecast Generator",LOG_PID,LOG_USER);
79                   break;
80     case _file: if (object->GetValue(_logpath) != NULL)
81                   strcpy(int_buf,object->GetValue(_logpath));
82                 else
83                   strcpy(int_buf,"/var/log/icegenerator.log");
84                 if ((LogFp = fopen(int_buf,"a")) == NULL)
85                 {
86                   printf("Warning: cannot create /var/log/icegenerator.log\nNo log will happen\n");
87                   LogSelected = _none;
88                 }
89                 break;
90   }
91 }
92 
93 
WriteLog(const char * msg,const char * data)94 void cLog::WriteLog(const char *msg, const char *data)
95 {
96   time_t now;
97 
98   switch (LogSelected)
99   {
100     case _none: break;
101     case _system: if (data == NULL)
102                     syslog(LOG_INFO,"%s\n",msg);
103                   else
104                     syslog(LOG_INFO,"%s %s\n",msg,data);
105                   break;
106     case _file: now = time(NULL);
107                 strcpy(int_buf,asctime(localtime(&now)));
108                 int_buf[strlen(int_buf)-1] = '\0';
109                 if (data == NULL)
110                   fprintf(LogFp,"%s: %s\n",int_buf,msg);
111                 else
112                   fprintf(LogFp,"%s: %s %s\n",int_buf,msg,data);
113                 fflush(LogFp);
114                 break;
115   }
116 }
117 
~cLog()118 cLog::~cLog()
119 {
120   switch (LogSelected)
121   {
122     case _none: break;
123     case _system: closelog();
124                   break;
125     case _file: fprintf(LogFp,"\n\n");
126                 fclose(LogFp);
127                 break;
128   }
129 
130   delete [] int_buf;
131 }
132