File: dlog.cpp

package info (click to toggle)
vfu 4.16%2Brepack-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 2,652 kB
  • ctags: 4,212
  • sloc: cpp: 16,503; ansic: 12,357; perl: 598; makefile: 564; sh: 61
file content (104 lines) | stat: -rw-r--r-- 2,026 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
/****************************************************************************
 *
 * (c) Vladi Belperchinov-Shabanski "Cade" 1996-2015
 * http://cade.datamax.bg/  <cade@biscom.net> <cade@bis.bg> <cade@datamax.bg>
 *
 * SEE `README',`LICENSE' OR `COPYING' FILE FOR LICENSE AND OTHER DETAILS!
 *
 ****************************************************************************/

#include <time.h>
#include <string.h>
#include "dlog.h"

TLogFile::TLogFile()
{
  f = NULL;
  log_fn[0] = 0;
  keep_open = 0;
  on_stdout = 0;
  on_stderr = 0;
}

TLogFile::~TLogFile()
{
  close();
}

void TLogFile::create( const char *fname, int pkeep_open )
{
  strcpy( log_fn, fname );
  f = NULL;
  keep_open = pkeep_open;
  open();
  fprintf( f, "\n" );
  if (!keep_open) close();
}

void TLogFile::open()
{
  if ( f ) fclose( f );
  f = fopen( log_fn, "at" );
}

void TLogFile::close()
{
  if ( f ) fclose( f );
  f = NULL;
}

void TLogFile::log( const char *fname, int line, const char *msg )
{
  char tmp[1024];
  if (!keep_open) open();

  time_t now;
  time(&now);
  char stime[32];
  strcpy(stime, asctime(localtime(&now)));
  if (stime[strlen(stime) - 1] == '\n') stime[strlen(stime) - 1] = 0;


  if ( fname == NULL || line == -1 )
    sprintf( tmp, "%s : %s", stime, msg);
  else
    sprintf( tmp, "%s [%10s:%-5d] %s", stime, fname, line, msg);

  while(tmp[strlen(tmp) - 1] == '\n') tmp[strlen(tmp) - 1] = 0;
  strcat( tmp, "\n" );

  fputs( tmp, f );
  if (on_stdout) fputs( tmp, stdout );
  if (on_stderr) fputs( tmp, stderr );

  if (!keep_open && f != NULL) close();

}

void TLogFile::log( const char *msg )
{
  log( NULL, -1, msg );
}

void TLogFile::log( const char *msg, int n )
{
  char tmp[255];
  sprintf( tmp, msg, n );
  log( NULL, -1, tmp );
}

void TLogFile::log( const char *msg, const char *arg )
{
  char tmp[255];
  sprintf( tmp, msg, arg );
  log( NULL, -1, tmp );
}

void TLogFile::log( const char *fname, int line, const char *msg, int n )
{
  char tmp[255];
  sprintf( tmp, msg, n );
  log( fname, line, tmp );
}