summaryrefslogtreecommitdiff
path: root/src/core/xblog.cpp
blob: 94430061b4658cf5717dce1630f041e1ea0c4734 (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*  xblog.cpp

XBase64 Software Library

Copyright (c) 1997,2003,2014,2022,2023 Gary A Kunkel

The xb64 software library is covered under the terms of the GPL Version 3, 2007 license.

Email Contact:

    XDB-devel@lists.sourceforge.net
    XDB-users@lists.sourceforge.net

*/


#include "xbase.h"
//#include <time.h>

#ifdef XB_LOGGING_SUPPORT

namespace xb{

/******************************************************************************/
//! @brief Constructor.
xbLog::xbLog() : xbFile( NULL ){


  // std::cout << "xbLog::xbLog(1) Directory = [" << GetLogDirectory() << "]\n";
  // std::cout << "xbLog::xbLog(1) Name = [" << GetLogFileName() << "]\n";

  SetDirectory( GetLogDirectory());
  SetFileName ( GetLogFileName());

  bLoggingStatus = xbFalse;
  lLogSize       = 100000;

  #ifdef XB_LOCKING_SUPPORT
  iShareMode = XB_MULTI_USER;
  #else
  iShareMode = XB_SINGLE_USER;
  #endif
}
/******************************************************************************/
//! @brief Constructor.
/*!
  \param sLogFileName - Log file name.
*/
xbLog::xbLog( const xbString & sLogFileName ) : xbFile( NULL ){
  if( sLogFileName.GetPathSeparator())
    SetFqFileName( sLogFileName );     // file name includes a path
  else
    SetFileName( sLogFileName );       // no file path

  bLoggingStatus = xbFalse;
  lLogSize       = 100000;

  #ifdef XB_LOCKING_SUPPORT
  iShareMode = XB_MULTI_USER;
  #else
  iShareMode = XB_SINGLE_USER;
  #endif

}
/******************************************************************************/
//! @brief Deconstructor.
xbLog::~xbLog(){
  xbFclose();
}
/******************************************************************************/
//! @brief Get the current log status
/*!
  \returns xbTrue - Logging turned on.<br>xbFalse - Logging turned off.
*/
xbBool xbLog::LogGetStatus(){
  return bLoggingStatus;
}
/******************************************************************************/
//! @brief Close the logfile.
/*!
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbLog::LogClose(){
  return xbFclose();
}
/******************************************************************************/
//! @brief Set maximum log file size.
/*!
  \param lSize - New maximum log file size.
  \returns void
*/
void xbLog::LogSetLogSize( size_t lSize ){
  lLogSize = lSize;
}
/******************************************************************************/
//! @brief Set log status.
/*!
  \param bStatus xbTrue - Turn logging on.<br>xbFalse - Turn logging off.
  \returns void
*/
void xbLog::LogSetStatus( xbBool bStatus ){
  if( bLoggingStatus && !bStatus )
    LogClose();
  bLoggingStatus = bStatus;
}
/******************************************************************************/
//! @brief Open the logfile.
/*!
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/
xbInt16 xbLog::LogOpen(){
  xbInt16  rc;

//  std::cout << "*****\nxbLog::LogOpen(1) GetLogDirectory = " << GetLogDirectory() << "\n";
//  std::cout << "xbLog::LogOpen(1) GetLogFileName  = " << GetLogFileName() << "\n";
//  std::cout << "xbLog::GetFqFileName(1) = " << GetFqFileName() << "\n\n";

  // 4.1.3 added next two lines for dynamic log file name changing
  SetDirectory( GetLogDirectory());
  SetFileName ( GetLogFileName());

//  std::cout << "*****\nxbLog::LogOpen(2) GetLogDirectory = " << GetLogDirectory() << "\n";
//  std::cout << "xbLog::LogOpen(2) GetLogFileName  = " << GetLogFileName() << "\n";
//  std::cout << "xbLog::GetFqFileName(2) = " << GetFqFileName() << "\n\n";

  if(( rc = xbFopen( "a", iShareMode )) != XB_NO_ERROR )
    return rc;
  xbFTurnOffFileBuffering();
  return XB_NO_ERROR;
}
/******************************************************************************/
//! @brief Write a logfile message.
/*!
  \param sLogEntryData - Message to write to the logfile.
  \param iOutputOption 0 - Write to logfile.<br>
                       1 - Write to stdout.<br>
                       2 - Write to both logfile and stdout.
  \returns <a href="xbretcod_8h.html">Return Codes</a>
*/

xbInt16 xbLog::LogWrite( const xbString &sLogEntryData, xbInt16 iOutputOption ){

  if( bLoggingStatus == xbFalse ){    // logging turned off
    return XB_NO_ERROR;
  }
  xbInt16 rc = 0;
  if( iOutputOption != 1 && !FileIsOpen() ){
    if(( rc = LogOpen()) != XB_NO_ERROR ){
      fprintf( stderr, "Error - cant write to logfile\n" );
      return rc;
    }
  }
  if( iOutputOption != 1 && lLogSize < xbFtell()){
    xbFputs( "Swapping to next log file" );
    xbFclose();
    xbString sBackupName;
    sBackupName.Sprintf( "%s.bak", GetFqFileName().Str());
    if( FileExists( sBackupName ))
      xbRemove( sBackupName );

    xbRename( GetFqFileName(), sBackupName );
    xbFopen( "a", iShareMode );
  }
  xbString sTimeStamp;
  xbString sFled;     // formatted log entry data

  if( iOutputOption != 1 ){
    #ifdef HAVE__LOCALTIME64_S_F
      __time64_t timer;
      struct tm tb;
      _time64( &timer );
      _localtime64_s( &tb, &timer );
      tb.tm_year += 1900;
      tb.tm_mon++;
      sTimeStamp.Sprintf( "%4d-%02d-%02d %02d:%02d:%02d", tb.tm_year, tb.tm_mon, tb.tm_mday, tb.tm_hour, tb.tm_min, tb.tm_sec );
    #else
      time_t timer;
      struct tm *tb;
      timer = time( NULL );
      tb = localtime( &timer );
      tb->tm_year += 1900;
      tb->tm_mon++;
      sTimeStamp.Sprintf( "%4d-%02d-%02d %02d:%02d:%02d", tb->tm_year, tb->tm_mon, tb->tm_mday, tb->tm_hour, tb->tm_min, tb->tm_sec );
    #endif
    sFled.Sprintf( "%s - %s\n", sTimeStamp.Str(), sLogEntryData.Str() );
  }

  switch( iOutputOption ){
    case 0:
      xbFputs( sFled );
      break;
    case 1:
      std::cout << sLogEntryData << std::endl;
      break;
    case 2:
      xbFputs( sFled );
      std::cout << sLogEntryData << std::endl;
      break;
  }
  return XB_NO_ERROR;
}
/******************************************************************************/
//! @brief Write bytes to logfile.
/*!
  \param ulByteCnt - Number of bytes to write to logfile.
  \param p - Pointer to data to write to logfile.
  \returns XB_NO_ERROR
*/

xbInt16 xbLog::LogWriteBytes( xbUInt32 ulByteCnt, const char *p ){

  if( bLoggingStatus == xbFalse )    // logging turned off
    return XB_NO_ERROR;
  const char *p2 = p;
  xbFputc( '[' );
  for( xbUInt32 l = 0; l < ulByteCnt; l++ )
    xbFputc( *p2++ );
  xbFputc( ']' );
  return XB_NO_ERROR;
}
/******************************************************************************/
}         // namespace
#endif    // XB_LOGGING_ON