File: toollog.cpp

package info (click to toggle)
libguytools2 2.0.5-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 288 kB
  • sloc: cpp: 2,615; ansic: 288; makefile: 45; sh: 4
file content (178 lines) | stat: -rwxr-xr-x 5,314 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// ****************************************************************************
//  Project:        libguytools
// ****************************************************************************
//  Programmer:     Guy Voncken
//                  Police Grand-Ducale
//                  Service de Police Judiciaire
//                  Section Nouvelles Technologies
// ****************************************************************************
//  Module:         Log utility
// ****************************************************************************

// Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 
// Guy Voncken
//
// This file is part of libguytools.
//
// libguytools is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// libguytools is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libguytools. If not, see <http://www.gnu.org/licenses/>.

#ifndef _FILE_OFFSET_BITS
   #define _FILE_OFFSET_BITS 64
#endif

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>

#include <qmutex.h>

#include "toolglobalid.h"
#include "toolerror.h"
#include "toollog.h"


// -----------
//  Constants
// -----------

const char * LogLevelLookup [t_Log::Entries] = {"Debug", "Info", "Error"};
const int    LOG_HEADER_LEN = 75;

const char   LibGuyToolsVersion[] = LIBGUYTOOLS_VERSION;

// -----------------
//  Local variables
// -----------------

static bool    LogInitialised = false;
static QMutex  LogMutex;
static char  *pLogFilename;
static char    LogLineHeader[512];


t_Log::t_Log (const char* pFileName, APIRET &rc)
{
   if (!LogInitialised)
   {
      LogInitialised = true;
      pLogFilename = (char *) malloc (strlen (pFileName)+1);
      strcpy (pLogFilename, pFileName);
      Entry (Info, __FFL__, "Log file opened");
      rc = NO_ERROR;
   }
   else
   {
      rc = TOOLLOG_ERROR_INITIALISED_TWICE;
   }
}


t_Log::~t_Log(void)
{
   Entry (Info, __FFL__, "Log file closed");
   free (pLogFilename);
   LogInitialised = false;
}

bool t_Log::IsInitialised (void)
{
   return LogInitialised;
}


void t_Log::GetLibGuyToolsVersion (const char **pVersion)
{
   *pVersion = LibGuyToolsVersion;
}

void t_Log::vEntry (t_Level Level, const char *pFileName, const char *pFunctionName, int LineNr, const char *pFormat, va_list pArguments)
{
   time_t      NowT;
   struct tm *pNowTM;
   FILE      *pFile;
   int         wr;
   static bool LogFileError = false;  // Variable prevents us from repetitive log error messages

   time (&NowT);
   pNowTM = localtime (&NowT);
   LogMutex.lock();
      wr  = strftime (&LogLineHeader[0] , sizeof(LogLineHeader)   , "%a %d.%b.%Y %H:%M:%S ", pNowTM);
      wr += snprintf (&LogLineHeader[wr], sizeof(LogLineHeader)-wr, "%08X ", (unsigned int)pthread_self());
//      wr += snprintf (&LogLineHeader[wr], sizeof(LogLineHeader)-wr, "%4d ", getpid());  // getpid returns alyways the same number


      if (pFileName && pFunctionName)
         wr += snprintf (&LogLineHeader[wr], sizeof(LogLineHeader)-wr, "%s %s %d", pFileName, pFunctionName, LineNr);

      while (wr < LOG_HEADER_LEN)
         LogLineHeader[wr++] = ' ';
      snprintf (&LogLineHeader[wr], sizeof(LogLineHeader)-wr, "%-5s - ", LogLevelLookup[Level]);


      if (LogInitialised)
      {
         pFile = fopen64 (pLogFilename, "a");
         if (pFile == NULL)
         {
            if (!LogFileError)
               printf ("\nLog file error: Can't be opened");
            LogFileError = true;
         }
         else
         {
            LogFileError = false;
            fprintf  (pFile, "%s", &LogLineHeader[0]);
            vfprintf (pFile, pFormat, pArguments);
            fprintf  (pFile, "\n");
            fclose   (pFile);
         }
      }
      if (!LogInitialised || (pFile == NULL))
      {
         printf  ("\n");
         printf  ("%s", &LogLineHeader[0]);
         vprintf (pFormat, pArguments);
      }
   LogMutex.unlock();
}

void t_Log::Entry (t_Level Level, const char *pFileName, const char *pFunctionName, int LineNr, const char *pFormat, ...)
{
   va_list VaList;

   va_start(VaList, pFormat);
   vEntry (Level, pFileName, pFunctionName, LineNr, pFormat, VaList);
   va_end(VaList);
}

void t_Log::vEntryInfo (const char *pFileName, const char *pFunctionName, int LineNr, const char *pFormat, va_list pArguments)
{
   vEntry (Info, pFileName, pFunctionName, LineNr, pFormat, pArguments);
}

void t_Log::vEntryDebug (const char *pFileName, const char *pFunctionName, int LineNr, const char *pFormat, va_list pArguments)
{
   vEntry (Debug, pFileName, pFunctionName, LineNr, pFormat, pArguments);
}

void t_Log::vEntryError (const char *pFileName, const char *pFunctionName, int LineNr, const char *pFormat, va_list pArguments)
{
   vEntry (Error, pFileName, pFunctionName, LineNr, pFormat, pArguments);
}