2
* This file is part of nzbget
4
* Copyright (C) 2004 Sven Henkel <sidddy@users.sourceforge.net>
5
* Copyright (C) 2007-2009 Andrei Prygounkov <hugbug@users.sourceforge.net>
7
* This program 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.
12
* This program 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.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
* $Date: 2009-06-14 17:57:01 +0200 (Sun, 14 Jun 2009) $
48
extern Options* g_pOptions;
54
m_szLogFilename = NULL;
56
m_bExtraDebug = Util::FileExists("extradebug");
62
for (Messages::iterator it = m_Messages.begin(); it != m_Messages.end(); it++)
69
free(m_szLogFilename);
73
void Log::Filelog(const char* msg, ...)
81
vsnprintf(tmp2, 1024, msg, ap);
90
ctime_r(&rawtime, szTime, 50);
92
ctime_r(&rawtime, szTime);
95
szTime[strlen(szTime) - 1] = '\0'; // trim LF
97
FILE* file = fopen(m_szLogFilename, "ab+");
101
unsigned long iThreadId = GetCurrentThreadId();
103
unsigned long iThreadId = (unsigned long)pthread_self();
106
fprintf(file, "%s\t%lu\t%s%s", szTime, iThreadId, tmp2, LINE_ENDING);
108
fprintf(file, "%s\t%s%s", szTime, tmp2, LINE_ENDING);
114
perror(m_szLogFilename);
121
#ifdef HAVE_VARIADIC_MACROS
122
void debug(const char* szFilename, const char* szFuncname, int iLineNr, const char* msg, ...)
124
void debug(const char* msg, ...)
131
vsnprintf(tmp1, 1024, msg, ap);
136
#ifdef HAVE_VARIADIC_MACROS
139
snprintf(tmp2, 1024, "%s (%s:%i:%s)", tmp1, Util::BaseFileName(szFilename), iLineNr, szFuncname);
143
snprintf(tmp2, 1024, "%s (%s:%i)", tmp1, Util::BaseFileName(szFilename), iLineNr);
146
snprintf(tmp2, 1024, "%s", tmp1);
150
g_pLog->m_mutexLog.Lock();
152
if (!g_pOptions && g_pLog->m_bExtraDebug)
154
printf("%s\n", tmp2);
157
Options::EMessageTarget eMessageTarget = g_pOptions ? g_pOptions->GetDebugTarget() : Options::mtScreen;
158
if (eMessageTarget == Options::mtLog || eMessageTarget == Options::mtBoth)
160
g_pLog->Filelog("DEBUG\t%s", tmp2);
162
if (eMessageTarget == Options::mtScreen || eMessageTarget == Options::mtBoth)
164
g_pLog->AppendMessage(Message::mkDebug, tmp2);
167
g_pLog->m_mutexLog.Unlock();
171
void error(const char* msg, ...)
177
vsnprintf(tmp2, 1024, msg, ap);
181
g_pLog->m_mutexLog.Lock();
183
Options::EMessageTarget eMessageTarget = g_pOptions ? g_pOptions->GetErrorTarget() : Options::mtBoth;
184
if (eMessageTarget == Options::mtLog || eMessageTarget == Options::mtBoth)
186
g_pLog->Filelog("ERROR\t%s", tmp2);
188
if (eMessageTarget == Options::mtScreen || eMessageTarget == Options::mtBoth)
190
g_pLog->AppendMessage(Message::mkError, tmp2);
193
g_pLog->m_mutexLog.Unlock();
196
void warn(const char* msg, ...)
202
vsnprintf(tmp2, 1024, msg, ap);
206
g_pLog->m_mutexLog.Lock();
208
Options::EMessageTarget eMessageTarget = g_pOptions ? g_pOptions->GetWarningTarget() : Options::mtScreen;
209
if (eMessageTarget == Options::mtLog || eMessageTarget == Options::mtBoth)
211
g_pLog->Filelog("WARNING\t%s", tmp2);
213
if (eMessageTarget == Options::mtScreen || eMessageTarget == Options::mtBoth)
215
g_pLog->AppendMessage(Message::mkWarning, tmp2);
218
g_pLog->m_mutexLog.Unlock();
221
void info(const char* msg, ...)
227
vsnprintf(tmp2, 1024, msg, ap);
231
g_pLog->m_mutexLog.Lock();
233
Options::EMessageTarget eMessageTarget = g_pOptions ? g_pOptions->GetInfoTarget() : Options::mtScreen;
234
if (eMessageTarget == Options::mtLog || eMessageTarget == Options::mtBoth)
236
g_pLog->Filelog("INFO\t%s", tmp2);
238
if (eMessageTarget == Options::mtScreen || eMessageTarget == Options::mtBoth)
240
g_pLog->AppendMessage(Message::mkInfo, tmp2);
243
g_pLog->m_mutexLog.Unlock();
246
void detail(const char* msg, ...)
252
vsnprintf(tmp2, 1024, msg, ap);
256
g_pLog->m_mutexLog.Lock();
258
Options::EMessageTarget eMessageTarget = g_pOptions ? g_pOptions->GetDetailTarget() : Options::mtScreen;
259
if (eMessageTarget == Options::mtLog || eMessageTarget == Options::mtBoth)
261
g_pLog->Filelog("DETAIL\t%s", tmp2);
263
if (eMessageTarget == Options::mtScreen || eMessageTarget == Options::mtBoth)
265
g_pLog->AppendMessage(Message::mkDetail, tmp2);
268
g_pLog->m_mutexLog.Unlock();
271
void abort(const char* msg, ...)
277
vsnprintf(tmp2, 1024, msg, ap);
281
g_pLog->m_mutexLog.Lock();
283
printf("\n%s", tmp2);
285
g_pLog->Filelog(tmp2);
287
g_pLog->m_mutexLog.Unlock();
292
//************************************************************
295
Message::Message(unsigned int iID, EKind eKind, time_t tTime, const char* szText)
302
m_szText = strdup(szText);
318
void Log::AppendMessage(Message::EKind eKind, const char * szText)
320
Message* pMessage = new Message(++m_iIDGen, eKind, time(NULL), szText);
321
m_Messages.push_back(pMessage);
325
while (m_Messages.size() > (unsigned int)g_pOptions->GetLogBufferSize())
327
Message* pMessage = m_Messages.front();
329
m_Messages.pop_front();
334
Log::Messages* Log::LockMessages()
340
void Log::UnlockMessages()
347
remove(g_pOptions->GetLogFile());
351
* During intializing stage (when options were not read yet) all messages
352
* are saved in screen log, even if they shouldn't (according to options).
353
* Method "InitOptions()" check all messages added to screen log during
354
* intializing stage and does two things:
355
* 1) save the messages to log-file (if they should according to options);
356
* 2) delete messages from screen log (if they should not be saved in screen log).
358
void Log::InitOptions()
360
const char* szMessageType[] = { "INFO", "WARNING", "ERROR", "DEBUG", "DETAIL"};
362
if (g_pOptions->GetCreateLog() && g_pOptions->GetLogFile())
364
m_szLogFilename = strdup(g_pOptions->GetLogFile());
367
for (unsigned int i = 0; i < m_Messages.size(); )
369
Message* pMessage = m_Messages.at(i);
370
Options::EMessageTarget eTarget = Options::mtNone;
371
switch (pMessage->GetKind())
373
case Message::mkDebug:
374
eTarget = g_pOptions->GetDebugTarget();
376
case Message::mkDetail:
377
eTarget = g_pOptions->GetDetailTarget();
379
case Message::mkInfo:
380
eTarget = g_pOptions->GetInfoTarget();
382
case Message::mkWarning:
383
eTarget = g_pOptions->GetWarningTarget();
385
case Message::mkError:
386
eTarget = g_pOptions->GetErrorTarget();
390
if (eTarget == Options::mtLog || eTarget == Options::mtBoth)
392
Filelog("%s\t%s", szMessageType[pMessage->GetKind()], pMessage->GetText());
395
if (eTarget == Options::mtLog || eTarget == Options::mtNone)
398
m_Messages.erase(m_Messages.begin() + i);