]> git.vomp.tv Git - vompclient.git/blob - log.cc
German updates
[vompclient.git] / log.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
3     Copyright 2003-2004 University Of Bradford
4
5     This file is part of VOMP.
6
7     VOMP is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     VOMP is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with VOMP; if not, write to the Free Software
19     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 */
21
22 #include "log.h"
23
24 Log* Log::instance = NULL;
25
26 Log::Log()
27 {
28   if (instance) return;
29   instance = this;
30   logfile = NULL;
31   initted = 0;
32   logLevel = 0;
33 }
34
35 Log::~Log()
36 {
37   instance = NULL;
38 }
39
40 Log* Log::getInstance()
41 {
42   return instance;
43 }
44
45 void Log::upLogLevel()
46 {
47   if (logLevel == Log::DEBUG)
48   {
49     log("Log", logLevel, "Log level is at its highest already");
50     return;
51   }
52
53   logLevel++;
54   log("Log", logLevel, "Log level is now %i", logLevel);
55 }
56
57 void Log::downLogLevel()
58 {
59   if (logLevel == Log::CRAZY)
60   {
61     log("Log", logLevel, "Log level is at its lowest already");
62     return;
63   }
64
65   logLevel--;
66   log("Log", logLevel, "Log level is now %i", logLevel);
67 }
68
69 int Log::init(int startLogLevel, char* fileName, int tenabled)
70 {
71   initted = 1;
72   logLevel = startLogLevel;
73   enabled = tenabled;
74 //  logfile = fopen(fileName, "a");
75 //  logfile = fopen(stdout, "a");
76   logfile = stdout;
77 //  logfile = fopen("/log", "a");
78
79   if (logfile) return 1;
80   else return 0;
81 }
82
83 int Log::shutdown()
84 {
85   if (!initted) return 1;
86   if (enabled) fclose(logfile);
87   return 1;
88 }
89
90 int Log::log(char *fromModule, int level, char* message, ...)
91 {
92   if (!instance || !logfile) return 0;
93
94   if (!enabled) return 1;
95   if (level > logLevel) return 1;
96
97   char buffer[151];
98   int spaceLeft = 150;
99
100 #ifndef _MSC_VER
101   struct timeval tv;
102   gettimeofday(&tv, NULL);
103   struct tm* tms = localtime(&tv.tv_sec);
104 #else
105   struct _timeb tb;
106   _ftime(&tb);
107   struct tm* tms = localtime(&tb.time);
108 #endif
109   spaceLeft -= strftime(buffer, spaceLeft, "%H:%M:%S.", tms);
110 #ifndef _MSC_VER
111   spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tv.tv_usec);
112 #else
113   spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%06lu ", (unsigned long)tb.millitm);
114 #endif
115
116   char levelString[10];
117   if (level == CRAZY)   strcpy(levelString, "[CRAZY] ");
118   if (level == EMERG)   strcpy(levelString, "[EMERG] ");
119   if (level == ALERT)   strcpy(levelString, "[ALERT] ");
120   if (level == CRIT)    strcpy(levelString, "[CRIT]  ");
121   if (level == ERR)     strcpy(levelString, "[ERR]   ");
122   if (level == WARN)    strcpy(levelString, "[WARN]  ");
123   if (level == NOTICE)  strcpy(levelString, "[notice]");
124   if (level == INFO)    strcpy(levelString, "[info]  ");
125   if (level == DEBUG)   strcpy(levelString, "[debug] ");
126
127 #ifndef WIN32
128   spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%s %d %s - ", levelString, getpid(), fromModule);
129 #else
130   spaceLeft -= SNPRINTF(&buffer[150-spaceLeft], spaceLeft, "%s %s - ", levelString,  fromModule);
131 #endif
132
133   va_list ap;
134   va_start(ap, message);
135   spaceLeft = VSNPRINTF(&buffer[150-spaceLeft], spaceLeft, message, ap);
136   va_end(ap);
137
138   int messageLength = strlen(buffer);
139   if (messageLength < 150)
140   {
141     buffer[messageLength] = '\n';
142     buffer[messageLength+1] = '\0';
143   }
144   else
145   {
146     buffer[149] = '\n';
147     buffer[150] = '\0';
148   }
149
150   int success = fputs(buffer, logfile);
151   fflush(NULL);
152
153   if (success != EOF)
154     return 1;
155   else
156     return 0;
157
158 }
159
160 int Log::status()
161 {
162   if (instance && logfile) return 1;
163   else return 0;
164 }