| 1 | // **************************************************************************** |
| 2 | // Project: libguytools |
| 3 | // **************************************************************************** |
| 4 | // Programmer: Guy Voncken |
| 5 | // Police Grand-Ducale |
| 6 | // Service de Police Judiciaire |
| 7 | // Section Nouvelles Technologies |
| 8 | // **************************************************************************** |
| 9 | // Module: Log utility |
| 10 | // **************************************************************************** |
| 11 | |
| 12 | // Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, |
| 13 | // 2018, 2019 |
| 14 | // Guy Voncken |
| 15 | // |
| 16 | // This file is part of libguytools. |
| 17 | // |
| 18 | // libguytools 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 | // libguytools 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 libguytools. If not, see <http://www.gnu.org/licenses/>. |
| 30 | |
| 31 | #ifndef _FILE_OFFSET_BITS |
| 32 | #define _FILE_OFFSET_BITS 64 |
| 33 | #endif |
| 34 | |
| 35 | #include <stdio.h> |
| 36 | #include <stdarg.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | #include <time.h> |
| 40 | #include <pthread.h> |
| 41 | #include <unistd.h> |
| 42 | |
| 43 | #include <qmutex.h> |
| 44 | |
| 45 | #include "toolglobalid.h" |
| 46 | #include "toolerror.h" |
| 47 | #include "toollog.h" |
| 48 | |
| 49 | |
| 50 | // ----------- |
| 51 | // Constants |
| 52 | // ----------- |
| 53 | |
| 54 | static const char * LogLevelLookup [t_Log::Entries] = {"Debug", "Info", "Error"}; |
| 55 | static const int LOG_HEADER_LEN = 75; |
| 56 | |
| 57 | static const char LibGuyToolsVersion[] = LIBGUYTOOLS_VERSION; |
| 58 | |
| 59 | // ----------------- |
| 60 | // Local variables |
| 61 | // ----------------- |
| 62 | |
| 63 | static bool LogInitialised = false; |
| 64 | static QMutex LogMutex; |
| 65 | static char *pLogFilename; |
| 66 | static char LogLineHeader[512]; |
| 67 | |
| 68 | |
| 69 | t_Log::t_Log (const char* pFileName, APIRET &rc) |
| 70 | { |
| 71 | if (!LogInitialised) |
| 72 | { |
| 73 | LogInitialised = true; |
| 74 | pLogFilename = (char *) malloc (strlen (pFileName)+1); |
| 75 | strcpy (pLogFilename, pFileName); |
| 76 | Entry (Info, __FFL__, "Log file opened"); |
| 77 | rc = NO_ERROR; |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | rc = TOOLLOG_ERROR_INITIALISED_TWICE; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | |
| 86 | t_Log::~t_Log(void) |
| 87 | { |
| 88 | Entry (Info, __FFL__, "Log file closed"); |
| 89 | free (pLogFilename); |
| 90 | LogInitialised = false; |
| 91 | } |
| 92 | |
| 93 | bool t_Log::IsInitialised (void) |
| 94 | { |
| 95 | return LogInitialised; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | void t_Log::GetLibGuyToolsVersion (const char **pVersion) |
| 100 | { |
| 101 | *pVersion = LibGuyToolsVersion; |
| 102 | } |
| 103 | |
| 104 | void t_Log::vEntry (t_Level Level, const char *pFileName, const char *pFunctionName, int LineNr, const char *pFormat, va_list pArguments) |
| 105 | { |
| 106 | time_t NowT; |
| 107 | struct tm *pNowTM; |
| 108 | FILE *pFile; |
| 109 | size_t wr; |
| 110 | static bool LogFileError = false; // Variable prevents us from repetitive log error messages |
| 111 | |
| 112 | time (&NowT); |
| 113 | pNowTM = localtime (&NowT); |
| 114 | LogMutex.lock(); |
| 115 | wr = strftime (&LogLineHeader[0] , sizeof(LogLineHeader) , "%a %d.%b.%Y %H:%M:%S ", pNowTM); |
| 116 | wr += (size_t) snprintf (&LogLineHeader[wr], sizeof(LogLineHeader)-wr, "%08X ", (unsigned int)pthread_self()); |
| 117 | // wr += snprintf (&LogLineHeader[wr], sizeof(LogLineHeader)-wr, "%4d ", getpid()); // getpid returns alyways the same number |
| 118 | |
| 119 | |
| 120 | if (pFileName && pFunctionName) |
| 121 | wr += snprintf (&LogLineHeader[wr], sizeof(LogLineHeader)-wr, "%s %s %d", pFileName, pFunctionName, LineNr); |
| 122 | |
| 123 | while (wr < LOG_HEADER_LEN) |
| 124 | LogLineHeader[wr++] = ' '; |
| 125 | snprintf (&LogLineHeader[wr], sizeof(LogLineHeader)-wr, "%-5s - ", LogLevelLookup[Level]); |
| 126 | |
| 127 | |
| 128 | if (LogInitialised) |
| 129 | { |
| 130 | pFile = fopen64 (pLogFilename, "a"); |
| 131 | if (pFile == nullptr) |
| 132 | { |
| 133 | if (!LogFileError) |
| 134 | printf ("\nLog file error: Can't be opened"); |
| 135 | LogFileError = true; |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | LogFileError = false; |
| 140 | fprintf (pFile, "%s", &LogLineHeader[0]); |
| 141 | vfprintf (pFile, pFormat, pArguments); |
| 142 | fprintf (pFile, "\n"); |
| 143 | fclose (pFile); |
| 144 | } |
| 145 | } |
| 146 | if (!LogInitialised || (pFile == nullptr)) |
| 147 | { |
| 148 | printf ("\n"); |
| 149 | printf ("%s", &LogLineHeader[0]); |
| 150 | vprintf (pFormat, pArguments); |
| 151 | } |
| 152 | LogMutex.unlock(); |
| 153 | } |
| 154 | |
| 155 | void t_Log::Entry (t_Level Level, const char *pFileName, const char *pFunctionName, int LineNr, const char *pFormat, ...) |
| 156 | { |
| 157 | va_list VaList; |
| 158 | |
| 159 | va_start(VaList, pFormat); |
| 160 | vEntry (Level, pFileName, pFunctionName, LineNr, pFormat, VaList); |
| 161 | va_end(VaList); |
| 162 | } |
| 163 | |
| 164 | void t_Log::vEntryInfo (const char *pFileName, const char *pFunctionName, int LineNr, const char *pFormat, va_list pArguments) |
| 165 | { |
| 166 | vEntry (Info, pFileName, pFunctionName, LineNr, pFormat, pArguments); |
| 167 | } |
| 168 | |
| 169 | void t_Log::vEntryDebug (const char *pFileName, const char *pFunctionName, int LineNr, const char *pFormat, va_list pArguments) |
| 170 | { |
| 171 | vEntry (Debug, pFileName, pFunctionName, LineNr, pFormat, pArguments); |
| 172 | } |
| 173 | |
| 174 | void t_Log::vEntryError (const char *pFileName, const char *pFunctionName, int LineNr, const char *pFormat, va_list pArguments) |
| 175 | { |
| 176 | vEntry (Error, pFileName, pFunctionName, LineNr, pFormat, pArguments); |
| 177 | } |
| 178 | |
| 179 |