summaryrefslogtreecommitdiff
path: root/ndb/src/common/logger/loggertest/LoggerUnitTest.cpp
blob: 990d2e0eada9e2746939b41b5831491ee4f244a7 (plain)
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
/* Copyright (C) 2003 MySQL AB

   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include "LoggerUnitTest.hpp"

#include <Logger.hpp>
#include <ConsoleLogHandler.hpp>
#include <FileLogHandler.hpp>

#if !defined NDB_OSE || !defined NDB_SOFTOSE
#include <SysLogHandler.hpp>
#endif

#include <NdbOut.hpp>
#include <NdbMain.h>

typedef bool (*TESTFUNC)(const char*);
typedef struct
{
  const char* name;
  TESTFUNC test;
}Tests;

static Tests testCases[] = { {"Alert", &LoggerUnitTest::testAlert},
			     {"Critical", &LoggerUnitTest::testCritical},
			     {"Error", &LoggerUnitTest::testError},
			     {"Warning", &LoggerUnitTest::testWarning},
			     {"Info", &LoggerUnitTest::testInfo},
			     {"Debug", &LoggerUnitTest::testDebug},
			     {"Info to Critical", &LoggerUnitTest::testInfoCritical},
			     {"All", &LoggerUnitTest::testAll},
			     {"Off", &LoggerUnitTest::testOff}
                           };

static Logger logger;
int testFailed = 0;

NDB_COMMAND(loggertest, "loggertest", "loggertest -console | -file", 
	    "loggertest", 16384)
{	
  if (argc < 2)
  {
#if defined NDB_OSE || defined NDB_SOFTOSE
    ndbout << "Usage: loggertest -console | -file" << endl;
#else
    ndbout << "Usage: loggertest -console | -file | -syslog" << endl;
#endif
    return 0;
  }

  if (strcmp(argv[1], "-console") == 0)
  {
    logger.createConsoleHandler();
  }
  else if (strcmp(argv[1], "-file") == 0)
  {
    logger.createFileHandler();
    //logger.addHandler(new FileLogHandler(argv[2]));
  }
#if !defined NDB_OSE || !defined NDB_SOFTOSE
  else if (strcmp(argv[1], "-syslog") == 0)
  {
    logger.createSyslogHandler();
  }
#endif

  logger.disable(Logger::LL_ALL);

  char str[256];
  int testCount = (sizeof(testCases) / sizeof(Tests)); 
  ndbout << "Starting " << testCount << " tests..." << endl;
  for (int i = 0; i < testCount; i++)
  {
    ndbout << "-- " << " Test " << i + 1 
         << " [" << testCases[i].name << "] --" << endl;
    BaseString::snprintf(str, 256, "%s %s %s %d", "Logging ", 
	       testCases[i].name, " message ", i);  
    if (testCases[i].test(str))
    {
      ndbout << "-- Passed --" << endl;
    }    
    else
    {
      ndbout << "-- Failed -- " << endl;
    }
    
  }
  ndbout << endl << "-- " << testCount - testFailed << " passed, " 
       << testFailed << " failed --" << endl;

  logger.removeAllHandlers(); // Need to remove all for OSE, 
                              // because logger is global
  return 0;  	
}

bool
LoggerUnitTest::logTo(Logger::LoggerLevel from, Logger::LoggerLevel to, const char* msg)
{
  logger.enable(from, to);
  return logTo(from, msg);
}

bool
LoggerUnitTest::logTo(Logger::LoggerLevel level, const char* msg)
{	
  logger.enable(level);
  logger.alert(msg);
  logger.critical(msg);
  logger.error(msg);
  logger.warning(msg);
  logger.info(msg);
  logger.debug(msg);	
  logger.disable(level);
  return true;
}

bool
LoggerUnitTest::testAll(const char* msg)
{	
  return logTo(Logger::LL_ALL, msg);
}

bool
LoggerUnitTest::testOff(const char* msg)
{
  return logTo(Logger::LL_OFF, msg);

}

bool
LoggerUnitTest::testAlert(const char* msg)
{
  return logTo(Logger::LL_ALERT, msg);
}

bool
LoggerUnitTest::testCritical(const char* msg)
{
  return logTo(Logger::LL_CRITICAL, msg);
}

bool
LoggerUnitTest::testError(const char* msg)
{
  return logTo(Logger::LL_ERROR, msg);
}

bool
LoggerUnitTest::testWarning(const char* msg)
{
  return logTo(Logger::LL_WARNING, msg);
}

bool
LoggerUnitTest::testInfo(const char* msg)
{
  return logTo(Logger::LL_INFO, msg);
}

bool
LoggerUnitTest::testDebug(const char* msg)
{
  return logTo(Logger::LL_DEBUG, msg);
}

bool 
LoggerUnitTest::testInfoCritical(const char* msg)
{
  return logTo(Logger::LL_CRITICAL, Logger::LL_INFO, msg);
}

void
LoggerUnitTest::error(const char* msg)
{
  testFailed++;
  ndbout << "Test failed: " << msg << endl;  
}

LoggerUnitTest::LoggerUnitTest()
{
}
LoggerUnitTest::~LoggerUnitTest()
{
}