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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
/*
* Copyright (c) 2007
* Shrew Soft Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Redistributions in any form must be accompanied by information on
* how to obtain complete source code for the software and any
* accompanying software that uses the software. The source code
* must either be included in the distribution or be available for no
* more than the cost of distribution plus a nominal fee, and must be
* freely redistributable under reasonable conditions. For an
* executable file, complete source code means the source code for all
* modules it contains. It does not include source code for modules or
* files that typically accompany the major components of the operating
* system on which the executable file runs.
*
* THIS SOFTWARE IS PROVIDED BY SHREW SOFT INC ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
* NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL SHREW SOFT INC
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
* AUTHOR : Matthew Grooms
* mgrooms@shrew.net
*
*/
#include "liblog.h"
_LOG::_LOG()
{
fp = NULL;
}
_LOG::~_LOG()
{
close();
}
bool _LOG::write_buff( char * buff, size_t size )
{
//
// build time stamp
//
char tbuff[ LOG_MAX_TXT ];
size_t tlen = 0;
if( !( log_flags & LOGFLAG_SYSTEM ) )
{
time_t ctime;
struct tm * ltime;
time( &ctime );
#ifdef WIN32
struct tm ltm;
ltime = <m;
localtime_s( ltime, &ctime );
#endif
#ifdef UNIX
ltime = localtime( &ctime );
#endif
tlen = strftime( tbuff, LOG_MAX_TXT, "%y/%m/%d %H:%M:%S ", ltime );
}
lock.lock();
//
// log buffer to console
//
if( log_flags & LOGFLAG_ECHO )
printf( "%s", buff );
//
// log individual lines
//
char * line = buff;
size_t oset = 0;
size_t llen;
while( line != NULL && line[ 0 ] )
{
char * next = strchr( line, '\n' );
if( next != NULL )
{
if( log_flags & LOGFLAG_SYSTEM )
next[ 0 ] = 0;
next++;
llen = next - line;
}
else
llen = strlen( line );
if( tlen )
write_line( tbuff, tlen );
write_line( line, llen );
line = next;
}
lock.unlock();
return true;
}
bool _LOG::write_line( char * buff, size_t size )
{
#ifdef WIN32
DWORD dwsize = ( DWORD ) size;
if( fp != NULL )
WriteFile(
fp,
buff,
dwsize,
&dwsize,
NULL );
#endif
#ifdef UNIX
if( log_flags & LOGFLAG_SYSTEM )
syslog( LOG_NOTICE, "%s", buff );
else
{
if( fp != NULL )
{
fwrite( buff, size, 1, fp );
fflush( fp );
}
}
#endif
return true;
}
bool _LOG::open( char * path, long level, long flags )
{
//
// set the log level
//
log_flags = flags;
log_level = level;
#ifdef WIN32
if( path )
{
close();
fp = CreateFile(
path,
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
0 );
if( fp == NULL )
return false;
}
#endif
#ifdef UNIX
if( path )
{
if( log_flags & LOGFLAG_SYSTEM )
openlog( path, LOG_NDELAY, LOG_DAEMON );
else
{
fp = fopen( path, "w" );
if( fp == NULL )
return false;
}
}
#endif
return true;
}
void _LOG::close()
{
#ifdef WIN32
if( fp != NULL )
{
FlushFileBuffers( fp );
CloseHandle( fp );
}
#endif
#ifdef UNIX
if( log_flags & LOGFLAG_SYSTEM )
closelog();
else
{
if( fp != NULL )
{
fflush( fp );
fclose( fp );
}
}
#endif
fp = NULL;
}
void _LOG::txt( long level, const char * fmt, ... )
{
char tbuff[ LOG_MAX_TXT ];
char fbuff[ LOG_MAX_TXT ];
if( level > log_level )
return;
va_list list;
va_start( list, fmt );
if( ( fp != NULL ) || ( log_flags & LOGFLAG_ECHO ) )
{
size_t tsize = LOG_MAX_TXT;
size_t tused = 0;
vsprintf_s( tbuff, LOG_MAX_TXT, fmt, list );
sprintf_s( fbuff, tsize, "%s", tbuff );
write_buff( fbuff, tused );
}
}
void _LOG::bin( long level, long blevel, void * bin, size_t len, const char * fmt, ... )
{
char tbuff[ LOG_MAX_TXT ];
char fbuff[ LOG_MAX_BIN ];
if( level > log_level )
return;
va_list list;
va_start( list, fmt );
if( ( fp != NULL ) || ( log_flags & LOGFLAG_ECHO ) )
{
// tsize = total buffer size - NLx2 - NULL
size_t tsize = LOG_MAX_BIN - 3;
size_t tused = 0;
// add our text label
vsprintf_s( tbuff, LOG_MAX_TXT, fmt, list );
tused += sprintf_s( fbuff, tsize, "%s ( %ld bytes )", tbuff, len );
// check binary log level
if( blevel <= log_level )
{
// setup target and source data pointers
char * tdata = fbuff;
char * sdata = ( char * ) bin;
// bsize = ( tsize / required chars per line ) * bin bytes per line
size_t ssize = ( ( tsize - tused ) / 77 ) * 32;
size_t sused = 0;
if( ssize > len )
ssize = len;
// format and log source bytes
for( ; sused < ssize; sused++ )
{
if( !( sused & 0x1F ) )
tused += sprintf_s( &tdata[ tused ], tsize - tused, "\n0x :" );
unsigned char bchar = sdata[ sused ];
if( !( sused & 0x03 ) )
tused += sprintf_s( &tdata[ tused ], tsize - tused, " %02x", bchar );
else
tused += sprintf_s( &tdata[ tused ], tsize - tused, "%02x", bchar );
assert( tsize > tused );
}
}
// add terminating null and append
tused += sprintf_s( &fbuff[ tused ], tsize - tused, "\n" );
write_buff( fbuff, tused );
}
return;
}
|