File: fglogger.cc

package info (click to toggle)
ftpgrab 0.1.2r-8
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 376 kB
  • ctags: 335
  • sloc: cpp: 2,790; makefile: 26
file content (165 lines) | stat: -rw-r--r-- 3,116 bytes parent folder | download | duplicates (8)
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
// fglogger.cc

#include "fglogger.h"

#ifndef _FGEXC_H
#include "fgexc.h"
#endif

#ifndef _FGGLOB_H
#include "fgglob.h"
#endif

#include <stdio.h>
#include <assert.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>

// Threading: FGLogger is a shared object so we need to serialize access
// to it
#include <pthread.h>

// Statics
FGString FGLogger::msFileName = "fglog.out";
FGLogger* FGLogger::mspLogger = 0;

// Synchronization object
// Should be private member static but can't forward declare
// pthread_mutex_t
static pthread_mutex_t msLogMutex;

void
FGLogger::SetLogName(const FGString& name)
{
  msFileName = name;
}

FGLogger&
FGLogger::GetLogger(void)
{
  if (mspLogger == 0) {
    mspLogger = new FGLogger(msFileName);
    pthread_mutex_init(&msLogMutex, NULL);
  }

  return *mspLogger;
}

FGLogger::FGLogger(const FGString& fileName)
{
  if (!FGGlob::gLogDisabled) {
    mpFile = fopen(fileName, "a");
    if (mpFile == NULL) {
      FGString details("`");
      details += fileName;
      details += '\'';
      throw FGException(FGException::kLogFileOpenFailed, details);
    }
  } else {
    mpFile = NULL;
  }
}

void
FGLogger::LogMsg(const FGString& msg, EFGLogSeverity severity,
                 bool locked)
{
  MutexAutoUnlocker autoLocker;

  // NB. It locked is set "true", client MUST have called public
  // ::Lock() method on the logger class!
  if (!locked) {
    autoLocker.Prime(&msLogMutex);
  }

  if (FGGlob::gVerbose) {
    printf("%s\n", (const char*)msg);
  }

  if (FGGlob::gLogDisabled || severity == kFGLLVerbose) {
    return;
  }

  FGString dateStamp = GetDateStamp();

  fprintf(mpFile, "%s %s\n", (const char*)dateStamp, (const char*)msg);
  fflush(mpFile);
}

void
FGLogger::LogException(const FGException& e, bool locked)
{
  MutexAutoUnlocker autoLocker;

  // NB. It locked is set "true", client MUST have called public
  // ::Lock() method on the logger class!
  if (!locked) {
    autoLocker.Prime(&msLogMutex);
  }

  // {ce} - 07-Dec-99: oops, forgot to log this to stdout
  FGString dateStamp = GetDateStamp();
  FGString excText = e.GetFullDetails();

  if (FGGlob::gVerbose)
  {
    printf("%s\n", (const char*)excText);
  }

  if (FGGlob::gLogDisabled) {
    return;
  }

  fprintf(mpFile, "%s %s\n", (const char*)dateStamp, (const char*)excText);
  fflush(mpFile);
}

FGString
FGLogger::GetDateStamp(void) const
{
  char dateBuf[64];
  struct timeval now;

  gettimeofday(&now, NULL);
  struct tm* pTime = localtime(&now.tv_sec);

  strftime(dateBuf, sizeof(dateBuf), "[%d %b %Y %H:%M]", pTime);

  FGString ret(dateBuf);
  return ret;
}

void
FGLogger::Lock(void)
{
  pthread_mutex_lock(&msLogMutex);
}

void
FGLogger::Unlock(void)
{
  pthread_mutex_unlock(&msLogMutex);
}

// Helper classes
FGLogger::MutexAutoUnlocker::MutexAutoUnlocker()
: mpMutex(0)
{
}

FGLogger::MutexAutoUnlocker::~MutexAutoUnlocker()
{
  if (mpMutex)
  {
    pthread_mutex_unlock(static_cast<pthread_mutex_t*>(mpMutex));
  }
}

void
FGLogger::MutexAutoUnlocker::Prime(void* pMutex)
{
  mpMutex = pMutex;
  pthread_mutex_lock(static_cast<pthread_mutex_t*>(mpMutex));
}