Skip to content

File Log.h

File List > detail > tools > Log.h

Go to the documentation of this file

// Copyright (c) 2012-2013, IGN France.
// Copyright (c) 2012-2022, Oslandia.
// SPDX-License-Identifier: LGPL-2.0-or-later

#ifndef _SFCGAL_LOG_H_
#define _SFCGAL_LOG_H_

#include "SFCGAL/config.h"

#include <boost/format.hpp>
#include <string>

#define SFCGAL_DEBUG(message)                                                  \
  SFCGAL::Logger::get()->log(SFCGAL::Logger::Debug, message, __FILE__, __LINE__)
#define SFCGAL_INFO(message)                                                   \
  SFCGAL::Logger::get()->log(SFCGAL::Logger::Info, message, __FILE__, __LINE__)
#define SFCGAL_WARNING(message)                                                \
  SFCGAL::Logger::get()->log(SFCGAL::Logger::Warning, message, __FILE__,       \
                             __LINE__)
#define SFCGAL_ERROR(message)                                                  \
  SFCGAL::Logger::get()->log(SFCGAL::Logger::Info, message, __FILE__, __LINE__)
#define SFCGAL_CRITICAL(message)                                               \
  SFCGAL::Logger::get()->log(SFCGAL::Logger::Critical, message, __FILE__,      \
                             __LINE__)

namespace SFCGAL {

class SFCGAL_API Logger {
public:
  ~Logger();

  typedef enum { Debug, Info, Warning, Error, Critical } Level;

  static Logger *
  get();

  void
  log(const Level &level, const boost::format &message,
      const std::string &filename = "", const int &lineNumber = -1);

  void
  log(const Level &level, const std::string &message,
      const std::string &filename = "", const int &lineNumber = -1);

  const Level &
  logLevel() const;
  void
  setLogLevel(const Level &logLevel);

private:
  Level _logLevel;
  bool _displayFilePosition;

  Logger(std::ostream &);
  Logger(const Logger &other);

  std::ostream _out;
};

SFCGAL_API Logger &
logger();

} // namespace SFCGAL

#define SFCGAL_LOG(level, msg)                                                 \
  do {                                                                         \
    SFCGAL::Logger::get() << "[" << (level) << " " << __FILE__ << ":"          \
                          << __LINE__ << "] " << msg << std::endl;             \
  } while (0)

#ifndef NDEBUG
#define LOG_DEBUG(msg)                                                         \
  do {                                                                         \
    SFCGAL_LOG("DEBUG", msg);                                                  \
  } while (0)
#else
#define LOG_DEBUG(msg)                                                         \
  do {                                                                         \
  } while (0)
#endif
#define LOG_NOTICE(msg)                                                        \
  do {                                                                         \
    SFCGAL_LOG("NOTICE", msg);                                                 \
  } while (0)
#define LOG_ERROR(msg)                                                         \
  do {                                                                         \
    SFCGAL_LOG("ERROR", msg);                                                  \
  } while (0)

#endif