hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
LogHandler.h
Go to the documentation of this file.
1 #ifndef LOGHANDLER_H
2 #define LOGHANDLER_H
3 #include <stdarg.h>
4 #include <Syslog.h>
5 #include <new>
6 #include <BaseHandler.h>
7 class LogHandler:public BaseHandler
8 {
9  public:
10  LogHandler(SysLog *sysLog):BaseHandler(101),log(sysLog){}
11  void* handle(char command, ...)
12  {
13  va_list list;
14  int *maxLevel = NULL;
15  setError(0, NULL);
16  va_start(list, command);
17  switch(command)
18  {
19  case 'a':
20  {//set max level
21  int state = va_arg(list, int);
22  maxLevel = new(std::nothrow) int;
23  if(!maxLevel)
24  {
25  setError(E_MEMORY, "Out of memory");
26  return NULL;
27  }
28  *maxLevel = log->getMaxLevel();
29  if(state >= LEV_EMERG && state <= LEV_DEBUG)
30  {
31  log->setMaxLevel(state);
32  }
33  else
34  {
35  setError(2, "Wrong level, value must be between 0 and 7");
36  }
37  break;
38  }
39  case 'b':
40  {//set level mask
41  int mask = va_arg(list, int);
42  maxLevel = new(std::nothrow) int;
43  if(!maxLevel)
44  {
45  setError(E_MEMORY, "Out of memory");
46  return NULL;
47  }
48  *maxLevel = log->getLevelMask();
49  if(mask <= 0xff)log->setLevelMask((u_int8_t)mask);
50  break;
51  }
52  case 'c':
53  {//get last error on level
54  int level = va_arg(list, int);
55  if(level >= LEV_EMERG && level <= LEV_DEBUG)
56  {
57  int errNo = 0;
58  char *data = log->getLastLog(level, errNo);
59  if(!data)data = strdup("");
60  char *ret = new(std::nothrow) char[strlen(data) + 1 + sizeof(int)];
61  if(!ret)
62  {
63  setError(E_MEMORY, "Out of memory");
64  return NULL;
65  }
66  *((int*)ret) = errNo;
67  memcpy(ret + sizeof(int), data, strlen(data) + 1);
68  free(data);
69  return ret;
70  }
71  else
72  {
73  setError(2, "Wrong level, value must be between 0 and 7");
74  }
75  break;
76  }
77  default:
78  {
79  setError(1, "Wrong command code");
80  }
81  }
82  va_end(list);
83  return maxLevel;
84  }
85 
86  private:
87  SysLog *log;
88 };
89 
90 #endif