VMware GemFire Native C++ Reference  9.1
Log.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #ifndef GEODE_LOG_H_
4 #define GEODE_LOG_H_
5 
6 /*
7  * Licensed to the Apache Software Foundation (ASF) under one or more
8  * contributor license agreements. See the NOTICE file distributed with
9  * this work for additional information regarding copyright ownership.
10  * The ASF licenses this file to You under the Apache License, Version 2.0
11  * (the "License"); you may not use this file except in compliance with
12  * the License. You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 
29 #include "geode_globals.hpp"
30 #include <cstdio>
31 #include <cstdarg>
32 
33 /******************************************************************************/
34 
35 #ifndef GEODE_HIGHEST_LOG_LEVEL
36 #define GEODE_HIGHEST_LOG_LEVEL All
37 #endif
38 
39 #ifndef GEODE_MAX_LOG_FILE_LIMIT
40 #define GEODE_MAX_LOG_FILE_LIMIT (1024 * 1024 * 1024)
41 #endif
42 
43 #ifndef GEODE_MAX_LOG_DISK_LIMIT
44 #define GEODE_MAX_LOG_DISK_LIMIT (1024ll * 1024ll * 1024ll * 1024ll)
45 #endif
46 
47 #define _GF_MSG_LIMIT 8192
48 
49 /******************************************************************************/
50 
54 namespace apache {
55 namespace geode {
56 namespace client {
57 
58 class Exception;
59 
60 /******************************************************************************/
61 /******************************************************************************/
62 
63 /* Logs the message if the given level is less than or equal to the current
64  * logging level. */
65 #define GF_LOG(level, expr) \
66  if (level > apache::geode::client::Log::logLevel()) { \
67  } else \
68  apache::geode::client::Log::log(level, expr)
69 
147  public:
148  /******/
149 
150  enum LogLevel {
151 
152  // NOTE: if you change this enum declaration at all, be sure to
153  // change levelToChars and charsToLevel functions!
154 
155  None,
156 
157  Error,
158  Warning,
159  Info,
160 
161  Default,
162 
163  Config,
164 
165  Fine,
166  Finer,
167  Finest,
168 
169  Debug,
170 
171  All
172 
173  };
174 
175  /******/
176 
180  static LogLevel logLevel() { return s_logLevel; }
181 
185  static void setLogLevel(LogLevel level) { s_logLevel = level; }
186 
192  static const char* logFileName();
193 
200  static void init
201  // 0 => use maximum value (currently 1G)
202  (LogLevel level, const char* logFileName, int32_t logFileLimit = 0,
203  int64_t logDiskSpaceLimit = 0);
204 
208  static void close();
209 
216  static const char* levelToChars(Log::LogLevel level);
217 
223  static LogLevel charsToLevel(const char* chars);
224 
239  static char* formatLogLine(char* buf, LogLevel level);
240 
241  /******/
242 
246  static bool enabled(LogLevel level) {
247  return (((s_doingDebug && level == Debug) ||
248  GEODE_HIGHEST_LOG_LEVEL >= level) &&
249  s_logLevel >= level);
250  }
251 
255  static void log(LogLevel level, const char* msg) {
256  if (enabled(level)) put(level, msg);
257  }
258 
262  static void logThrow(LogLevel level, const char* msg, const Exception& ex) {
263  if (enabled(level)) putThrow(level, msg, ex);
264  }
265 
269  static void logCatch(LogLevel level, const char* msg, const Exception& ex) {
270  if (enabled(level)) putCatch(level, msg, ex);
271  }
272 
273  /******/
274 
278  static bool errorEnabled() {
279  return GEODE_HIGHEST_LOG_LEVEL >= Error && s_logLevel >= Error;
280  }
281 
286  static void error(const char* msg) {
287  if (errorEnabled()) put(Error, msg);
288  }
289 
294  static void errorThrow(const char* msg, const Exception& ex) {
295  if (errorEnabled()) putThrow(Error, msg, ex);
296  }
297 
302  static void errorCatch(const char* msg, const Exception& ex) {
303  if (errorEnabled()) putCatch(Error, msg, ex);
304  }
305 
306  /******/
307 
311  static bool warningEnabled() {
312  return GEODE_HIGHEST_LOG_LEVEL >= Warning && s_logLevel >= Warning;
313  }
314 
319  static void warning(const char* msg) {
320  if (warningEnabled()) put(Warning, msg);
321  }
322 
327  static void warningThrow(const char* msg, const Exception& ex) {
328  if (warningEnabled()) putThrow(Warning, msg, ex);
329  }
330 
335  static void warningCatch(const char* msg, const Exception& ex) {
336  if (warningEnabled()) putCatch(Warning, msg, ex);
337  }
338 
339  /******/
340 
344  static bool infoEnabled() {
345  return GEODE_HIGHEST_LOG_LEVEL >= Info && s_logLevel >= Info;
346  }
347 
352  static void info(const char* msg) {
353  if (infoEnabled()) put(Info, msg);
354  }
355 
360  static void infoThrow(const char* msg, const Exception& ex) {
361  if (infoEnabled()) putThrow(Info, msg, ex);
362  }
363 
368  static void infoCatch(const char* msg, const Exception& ex) {
369  if (infoEnabled()) putCatch(Info, msg, ex);
370  }
371 
372  /******/
373 
377  static bool configEnabled() {
378  return GEODE_HIGHEST_LOG_LEVEL >= Config && s_logLevel >= Config;
379  }
380 
385  static void config(const char* msg) {
386  if (configEnabled()) put(Config, msg);
387  }
388 
393  static void configThrow(const char* msg, const Exception& ex) {
394  if (configEnabled()) putThrow(Config, msg, ex);
395  }
396 
401  static void configCatch(const char* msg, const Exception& ex) {
402  if (configEnabled()) putCatch(Config, msg, ex);
403  }
404 
405  /******/
406 
410  static bool fineEnabled() {
411  return GEODE_HIGHEST_LOG_LEVEL >= Fine && s_logLevel >= Fine;
412  }
413 
418  static void fine(const char* msg) {
419  if (fineEnabled()) put(Fine, msg);
420  }
421 
426  static void fineThrow(const char* msg, const Exception& ex) {
427  if (fineEnabled()) putThrow(Fine, msg, ex);
428  }
429 
434  static void fineCatch(const char* msg, const Exception& ex) {
435  if (fineEnabled()) putCatch(Fine, msg, ex);
436  }
437 
438  /******/
439 
443  static bool finerEnabled() {
444  return GEODE_HIGHEST_LOG_LEVEL >= Finer && s_logLevel >= Finer;
445  }
446 
451  static void finer(const char* msg) {
452  if (finerEnabled()) put(Finer, msg);
453  }
454 
459  static void finerThrow(const char* msg, const Exception& ex) {
460  if (finerEnabled()) putThrow(Finer, msg, ex);
461  }
462 
467  static void finerCatch(const char* msg, const Exception& ex) {
468  if (finerEnabled()) putCatch(Finer, msg, ex);
469  }
470 
471  /******/
472 
476  static bool finestEnabled() {
477  return GEODE_HIGHEST_LOG_LEVEL >= Finest && s_logLevel >= Finest;
478  }
479 
484  static void finest(const char* msg) {
485  if (finestEnabled()) put(Finest, msg);
486  }
487 
492  static void finestThrow(const char* msg, const Exception& ex) {
493  if (finestEnabled()) putThrow(Finest, msg, ex);
494  }
495 
500  static void finestCatch(const char* msg, const Exception& ex) {
501  if (finestEnabled()) putCatch(Finest, msg, ex);
502  }
503 
504  /******/
505 
509  static bool debugEnabled() {
510  return (s_doingDebug || GEODE_HIGHEST_LOG_LEVEL >= Debug) &&
511  s_logLevel >= Debug;
512  }
513 
518  static void debug(const char* msg) {
519  if (debugEnabled()) put(Debug, msg);
520  }
521 
526  static void debugThrow(const char* msg, const Exception& ex) {
527  if (debugEnabled()) putThrow(Debug, msg, ex);
528  }
529 
534  static void debugCatch(const char* msg, const Exception& ex) {
535  if (debugEnabled()) putCatch(Debug, msg, ex);
536  }
537 
538  /******/
539 
540  static void enterFn(LogLevel level, const char* functionName);
541 
542  static void exitFn(LogLevel level, const char* functionName);
543 
544  /******/
545 
546  private:
547  static LogLevel s_logLevel;
548 
549 /******/
550 
551 #ifdef DEBUG
552  enum { s_doingDebug = 1 };
553 #else
554  enum { s_doingDebug = 0 };
555 #endif
556 
557  /******/
558 
559  static void writeBanner();
560 
561  /******/
562  public:
563  static void put(LogLevel level, const char* msg);
564 
565  static void putThrow(LogLevel level, const char* msg, const Exception& ex);
566 
567  static void putCatch(LogLevel level, const char* msg, const Exception& ex);
568 };
569 
570 /******************************************************************************/
571 /******************************************************************************/
572 
573 class LogFn {
574  const char* m_functionName;
575  Log::LogLevel m_level;
576 
577  public:
578  LogFn(const char* functionName, Log::LogLevel level = Log::Finest)
579  : m_functionName(functionName), m_level(level) {
580  if (Log::enabled(m_level)) Log::enterFn(m_level, m_functionName);
581  }
582 
583  ~LogFn() {
584  if (Log::enabled(m_level)) Log::exitFn(m_level, m_functionName);
585  }
586 
587  private:
588  LogFn(const LogFn& rhs); // never defined
589  void operator=(const LogFn& rhs); // never defined
590 };
591 
592 /******************************************************************************/
593 /******************************************************************************/
594 
600  public:
601  static void debug(const char* fmt, ...);
602  static void error(const char* fmt, ...);
603  static void warn(const char* fmt, ...);
604  static void info(const char* fmt, ...);
605  static void config(const char* fmt, ...);
606  static void fine(const char* fmt, ...);
607  static void finer(const char* fmt, ...);
608  static void finest(const char* fmt, ...);
609 };
610 } // namespace client
611 } // namespace geode
612 } // namespace apache
613 
614 /************************ LOGDEBUG ***********************************/
615 
616 #define LOGDEBUG \
617  if (apache::geode::client::Log::Debug <= \
618  apache::geode::client::Log::logLevel()) \
619  apache::geode::client::LogVarargs::debug
620 
621 /************************ LOGERROR ***********************************/
622 
623 #define LOGERROR \
624  if (apache::geode::client::Log::Error <= \
625  apache::geode::client::Log::logLevel()) \
626  apache::geode::client::LogVarargs::error
627 
628 /************************ LOGWARN ***********************************/
629 
630 #define LOGWARN \
631  if (apache::geode::client::Log::Warning <= \
632  apache::geode::client::Log::logLevel()) \
633  apache::geode::client::LogVarargs::warn
634 
635 /************************ LOGINFO ***********************************/
636 
637 #define LOGINFO \
638  if (apache::geode::client::Log::Info <= \
639  apache::geode::client::Log::logLevel()) \
640  apache::geode::client::LogVarargs::info
641 
642 /************************ LOGCONFIG ***********************************/
643 
644 #define LOGCONFIG \
645  if (apache::geode::client::Log::Config <= \
646  apache::geode::client::Log::logLevel()) \
647  apache::geode::client::LogVarargs::config
648 
649 /************************ LOGFINE ***********************************/
650 
651 #define LOGFINE \
652  if (apache::geode::client::Log::Fine <= \
653  apache::geode::client::Log::logLevel()) \
654  apache::geode::client::LogVarargs::fine
655 
656 /************************ LOGFINER ***********************************/
657 
658 #define LOGFINER \
659  if (apache::geode::client::Log::Finer <= \
660  apache::geode::client::Log::logLevel()) \
661  apache::geode::client::LogVarargs::finer
662 
663 /************************ LOGFINEST ***********************************/
664 
665 #define LOGFINEST \
666  if (apache::geode::client::Log::Finest <= \
667  apache::geode::client::Log::logLevel()) \
668  apache::geode::client::LogVarargs::finest
669 
670 /******************************************************************************/
671 
672 /******************************************************************************/
673 
674 #endif // GEODE_LOG_H_
static void fine(const char *msg)
Logs a message.
Definition: Log.hpp:418
static void log(LogLevel level, const char *msg)
Logs a message at given level.
Definition: Log.hpp:255
static void finestThrow(const char *msg, const Exception &ex)
Logs both a message and thrown exception.
Definition: Log.hpp:492
static bool enabled(LogLevel level)
Returns whether log messages at given level are enabled.
Definition: Log.hpp:246
static void errorCatch(const char *msg, const Exception &ex)
Writes both a message and caught exception.
Definition: Log.hpp:302
Each enum represents a predefined RegionAttributes in a Cache.
Definition: Assert.hpp:31
static bool infoEnabled()
Returns whether "info" log messages are enabled.
Definition: Log.hpp:344
static void warning(const char *msg)
Logs a message.
Definition: Log.hpp:319
#define CPPCACHE_EXPORT
Defines a Geode CPPCACHE export.
Definition: geode_base.hpp:58
static void configThrow(const char *msg, const Exception &ex)
Logs both a message and thrown exception.
Definition: Log.hpp:393
#define GEODE_HIGHEST_LOG_LEVEL
The interface of the Log class.
Definition: Log.hpp:36
static void finestCatch(const char *msg, const Exception &ex)
Writes both a message and caught exception.
Definition: Log.hpp:500
static void configCatch(const char *msg, const Exception &ex)
Writes both a message and caught exception.
Definition: Log.hpp:401
static bool debugEnabled()
Returns whether "debug" log messages are enabled.
Definition: Log.hpp:509
static void setLogLevel(LogLevel level)
Set the current log level.
Definition: Log.hpp:185
static void infoThrow(const char *msg, const Exception &ex)
Logs both a message and thrown exception.
Definition: Log.hpp:360
static bool configEnabled()
Returns whether "config" log messages are enabled.
Definition: Log.hpp:377
static bool warningEnabled()
Returns whether "warning" log messages are enabled.
Definition: Log.hpp:311
static void config(const char *msg)
Logs a message.
Definition: Log.hpp:385
static void info(const char *msg)
Logs a message.
Definition: Log.hpp:352
static void finerThrow(const char *msg, const Exception &ex)
Logs both a message and thrown exception.
Definition: Log.hpp:459
static void error(const char *msg)
Logs a message.
Definition: Log.hpp:286
static void fineCatch(const char *msg, const Exception &ex)
Writes both a message and caught exception.
Definition: Log.hpp:434
static void logCatch(LogLevel level, const char *msg, const Exception &ex)
Logs both a message and caught exception.
Definition: Log.hpp:269
static void fineThrow(const char *msg, const Exception &ex)
Logs both a message and thrown exception.
Definition: Log.hpp:426
static void debug(const char *msg)
Logs a message.
Definition: Log.hpp:518
static void errorThrow(const char *msg, const Exception &ex)
Logs both a message and thrown exception.
Definition: Log.hpp:294
static void infoCatch(const char *msg, const Exception &ex)
Writes both a message and caught exception.
Definition: Log.hpp:368
static void debugCatch(const char *msg, const Exception &ex)
Writes both a message and caught exception.
Definition: Log.hpp:534
A description of an exception that occurred during a cache operation.
Definition: Exception.hpp:45
static void debugThrow(const char *msg, const Exception &ex)
Logs both a message and thrown exception.
Definition: Log.hpp:526
static bool errorEnabled()
Returns whether "error" log messages are enabled.
Definition: Log.hpp:278
static void finer(const char *msg)
Logs a message.
Definition: Log.hpp:451
static void warningThrow(const char *msg, const Exception &ex)
Logs both a message and thrown exception.
Definition: Log.hpp:327
static void finest(const char *msg)
Logs a message.
Definition: Log.hpp:484
These functions are added to facilitate logging in printf format.
Definition: Log.hpp:599
static bool finestEnabled()
Returns whether "finest" log messages are enabled.
Definition: Log.hpp:476
static LogLevel logLevel()
Returns the current log level.
Definition: Log.hpp:180
static void logThrow(LogLevel level, const char *msg, const Exception &ex)
Logs both a message and thrown exception.
Definition: Log.hpp:262
static void finerCatch(const char *msg, const Exception &ex)
Writes both a message and caught exception.
Definition: Log.hpp:467
static bool finerEnabled()
Returns whether "finer" log messages are enabled.
Definition: Log.hpp:443
static bool fineEnabled()
Returns whether "fine" log messages are enabled.
Definition: Log.hpp:410
Defines methods available to clients that want to write a log message to their Geode system&#39;s shared ...
Definition: Log.hpp:146
static void warningCatch(const char *msg, const Exception &ex)
Writes both a message and caught exception.
Definition: Log.hpp:335
This namespace contains all the Geode C++ API classes, enumerations and globals.

Pivotal GemFire C++ Cache API Documentation