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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
|
/*
* SPRTF - Log output utility
*
* Author: Geoff R. McLane <reports _at_ geoffair _dot_ info>
* License: GPL v2 (or later at your choice)
*
* Revision 1.0.1 2012/11/06 13:01:25 geoff
* Revision 1.0.0 2012/10/17 00:00:00 geoff
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
*
*/
// Module: sprtf.cxx
// Debug log file output
#include <stdio.h> // fopen()...
#include <string.h> // strcpy
#include <stdarg.h> // va_start, va_end, ...
#ifdef _MSC_VER
#include <WinSock2.h>
#include <sys/timeb.h>
#else /* !_MSC_VER */
#include <sys/time.h> // gettimeoday(), struct timeval,...
#endif /* _MSC_VER y/n */
#include <time.h>
#include <stdlib.h> // for exit() in unix
#include "sprtf.h"
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE
#endif // #ifndef _CRT_SECURE_NO_DEPRECATE
#pragma warning( disable:4996 )
#else
#define strcmpi strcasecmp
#endif
#ifndef MX_ONE_BUF
#define MX_ONE_BUF 1024
#endif
#ifndef MX_BUFFERS
#define MX_BUFFERS 1024
#endif
static char _s_strbufs[MX_ONE_BUF * MX_BUFFERS];
static int iNextBuf = 0;
char *GetNxtBuf()
{
iNextBuf++;
if(iNextBuf >= MX_BUFFERS)
iNextBuf = 0;
return &_s_strbufs[MX_ONE_BUF * iNextBuf];
}
#define MXIO 512
#ifdef _MSC_VER // use local log
static char def_log[] = "tempex.txt";
#else
static char def_log[] = "ex.log";
#endif
static char logfile[264] = "\0";
static FILE * outfile = NULL;
static int addsystime = 0;
static int addsysdate = 0;
static int addstdout = 1;
static int addflush = 1;
static int add2screen = 0;
static int add2listview = 0;
static int append_to_log = 0;
#ifndef VFP
#define VFP(a) ( a && ( a != (FILE *)-1 ) )
#endif
int add_list_out( int val )
{
int i = add2listview;
add2listview = val;
return i;
}
int add_std_out( int val )
{
int i = addstdout;
addstdout = val;
return i;
}
int add_screen_out( int val )
{
int i = add2screen;
add2screen = val;
return i;
}
int add_sys_time( int val )
{
int i = addsystime;
addsystime = val;
return i;
}
int add_sys_date( int val )
{
int i = addsysdate;
addsysdate = val;
return i;
}
int add_append_log( int val )
{
int i = append_to_log;
append_to_log = val;
return i;
}
#ifdef _MSC_VER
static const char *mode = "wb"; // in window sprtf looks after the line endings
#else
static const char *mode = "w";
#endif
int open_log_file( void )
{
if (logfile[0] == 0)
strcpy(logfile,def_log);
if (append_to_log) {
#ifdef _MSC_VER
mode = "ab"; // in window sprtf looks after the line endings
#else
mode = "a";
#endif
}
outfile = fopen(logfile, mode);
if( outfile == 0 ) {
outfile = (FILE *)-1;
sprtf("ERROR: Failed to open log file [%s] ...\n", logfile);
exit(1); /* failed */
return 0; /* failed */
}
return 1; /* success */
}
void close_log_file( void )
{
if( VFP(outfile) ) {
fclose(outfile);
}
outfile = NULL;
}
char * get_log_file( void )
{
if (logfile[0] == 0)
strcpy(logfile,def_log);
if (outfile == (FILE *)-1) // disable the log file
return (char *)"none";
return logfile;
}
void set_log_file( char * nf, int open )
{
if (logfile[0] == 0)
strcpy(logfile,def_log);
if ( nf && *nf && strcmpi(nf,logfile) ) {
close_log_file(); // remove any previous
strcpy(logfile,nf); // set new name
if (strcmp(logfile,"none") == 0) { // if equal 'none'
outfile = (FILE *)-1; // disable the log file
} else if (open) {
open_log_file(); // and open it ... anything previous written is 'lost'
} else
outfile = 0; // else set 0 to open on first write
}
}
#ifdef _MSC_VER
int gettimeofday(struct timeval *tp, void *tzp)
{
#ifdef WIN32
struct _timeb timebuffer;
_ftime(&timebuffer);
tp->tv_sec = (long)timebuffer.time;
tp->tv_usec = timebuffer.millitm * 1000;
#else
tp->tv_sec = time(NULL);
tp->tv_usec = 0;
#endif
return 0;
}
#endif // _MSC_VER
void add_date_stg( char *ps, struct timeval *ptv )
{
time_t curtime;
struct tm * ptm;
curtime = (ptv->tv_sec & 0xffffffff);
ptm = localtime(&curtime);
if (ptm) {
strftime(EndBuf(ps),128,"%Y/%m/%d",ptm);
}
}
void add_time_stg( char *ps, struct timeval *ptv )
{
time_t curtime;
struct tm * ptm;
curtime = (ptv->tv_sec & 0xffffffff);
ptm = localtime(&curtime);
if (ptm) {
strftime(EndBuf(ps),128,"%H:%M:%S",ptm);
}
}
char *get_date_stg()
{
char *ps;
struct timeval tv;
gettimeofday( (struct timeval *)&tv, (struct timezone *)0 );
ps = GetNxtBuf();
*ps = 0;
add_date_stg( ps, &tv );
return ps;
}
char *get_time_stg()
{
char *ps;
struct timeval tv;
gettimeofday( (struct timeval *)&tv, (struct timezone *)0 );
ps = GetNxtBuf();
*ps = 0;
add_time_stg( ps, &tv );
return ps;
}
char *get_date_time_stg()
{
char *ps;
struct timeval tv;
gettimeofday( (struct timeval *)&tv, (struct timezone *)0 );
ps = GetNxtBuf();
*ps = 0;
add_date_stg( ps, &tv );
strcat(ps," ");
add_time_stg( ps, &tv );
return ps;
}
static void oi( char * psin )
{
int len, w;
char * ps = psin;
if (!ps)
return;
len = (int)strlen(ps);
if (len) {
if( outfile == 0 ) {
open_log_file();
}
if( VFP(outfile) ) {
char *tb;
if (addsysdate) {
tb = GetNxtBuf();
len = sprintf( tb, "%s - %s", get_date_time_stg(), ps );
ps = tb;
} else if( addsystime ) {
tb = GetNxtBuf();
len = sprintf( tb, "%s - %s", get_time_stg(), ps );
ps = tb;
}
w = (int)fwrite( ps, 1, len, outfile );
if( w != len ) {
fclose(outfile);
outfile = (FILE *)-1;
sprtf("WARNING: Failed write to log file [%s] ...\n", logfile);
exit(1);
} else if (addflush) {
fflush( outfile );
}
}
if( addstdout ) {
fwrite( ps, 1, len, stdout );
}
#ifdef ADD_LISTVIEW
if (add2listview) {
LVInsertItem(ps);
}
#endif // ADD_LISTVIEW
#ifdef ADD_SCREENOUT
if (add2screen) {
Add_String(ps); // add string to screen list
}
#endif // #ifdef ADD_SCREENOUT
}
}
#ifdef _MSC_VER
// service to ensure line endings in windows only
static void prt( char * ps )
{
static char _s_buf[1024];
char * pb = _s_buf;
size_t i, j, k;
char c, d;
i = strlen(ps);
k = 0;
d = 0;
if(i) {
k = 0;
d = 0;
for( j = 0; j < i; j++ ) {
c = ps[j];
if( c == 0x0d ) {
if( (j+1) < i ) {
if( ps[j+1] != 0x0a ) {
pb[k++] = c;
c = 0x0a;
}
} else {
pb[k++] = c;
c = 0x0a;
}
} else if( c == 0x0a ) {
if( d != 0x0d ) {
pb[k++] = 0x0d;
}
}
pb[k++] = c;
d = c;
if( k >= MXIO ) {
pb[k] = 0;
oi(pb);
k = 0;
}
} // for length of string
if( k ) {
//if( ( gbCheckCrLf ) &&
// ( d != 0x0a ) ) {
// add Cr/Lf pair
//pb[k++] = 0x0d;
//pb[k++] = 0x0a;
//pb[k] = 0;
//}
pb[k] = 0;
oi( pb );
}
}
}
#endif // #ifdef _MSC_VER
int direct_out_it( char *cp )
{
#ifdef _MSC_VER
prt(cp);
#else
oi(cp);
#endif
return (int)strlen(cp);
}
// STDAPI StringCchVPrintf( OUT LPTSTR pszDest,
// IN size_t cchDest, IN LPCTSTR pszFormat, IN va_list argList );
int MCDECL sprtf( const char *pf, ... )
{
static char _s_sprtfbuf[M_MAX_SPRTF+4];
char * pb = _s_sprtfbuf;
int i;
va_list arglist;
va_start(arglist, pf);
i = vsprintf( pb, pf, arglist );
va_end(arglist);
#ifdef _MSC_VER
prt(pb); // ensure CR/LF
#else
oi(pb);
#endif
return i;
}
#ifdef UNICODE
// WIDE VARIETY
static void wprt( PTSTR ps )
{
static char _s_woibuf[1024];
char * cp = _s_woibuf;
int len = (int)lstrlen(ps);
if(len) {
int ret = WideCharToMultiByte( CP_ACP, // UINT CodePage, // code page
0, // DWORD dwFlags, // performance and mapping flags
ps, // LPCWSTR lpWideCharStr, // wide-character string
len, // int cchWideChar, // number of chars in string.
cp, // LPSTR lpMultiByteStr, // buffer for new string
1024, // int cbMultiByte, // size of buffer
NULL, // LPCSTR lpDefaultChar, // default for unmappable chars
NULL ); // LPBOOL lpUsedDefaultChar // set when default char used
//oi(cp);
prt(cp);
}
}
int MCDECL wsprtf( PTSTR pf, ... )
{
static WCHAR _s_sprtfwbuf[1024];
PWSTR pb = _s_sprtfwbuf;
int i = 1;
va_list arglist;
va_start(arglist, pf);
*pb = 0;
StringCchVPrintf(pb,1024,pf,arglist);
//i = vswprintf( pb, pf, arglist );
va_end(arglist);
wprt(pb);
return i;
}
#endif // #ifdef UNICODE
// eof - sprtf.cxx
|