Skip to content

include/tenzr/common/logging.h

Namespaces

Name
tenzr

Defines

Name
TENZR_LOG(lvl, prefix)
TENZR_LOG_DEBUG
Convenience TENZR_LOG wrappers for each severity level.
TENZR_LOG_INFO
TENZR_LOG_WARN
TENZR_LOG_ERROR

Macros Documentation

define TENZR_LOG

#define TENZR_LOG(
    lvl,
    prefix
)
  if (::tenzr::LogLevel::lvl >= ::tenzr::CurrentLogLevel()) std::cerr << "[" #prefix "] [" << __FILE__ << ":" << __LINE__ << "] "

Parameters:

  • lvl The message severity (Debug, Info, Warn, Error).
  • prefix The uppercase tag printed in the prefix.

Emits a [lvl] [file:line] prefix to std::cerr when lvl meets the current log level.

define TENZR_LOG_DEBUG

#define TENZR_LOG_DEBUG TENZR_LOG(kDebug, DEBUG)

Convenience TENZR_LOG wrappers for each severity level.

define TENZR_LOG_INFO

#define TENZR_LOG_INFO TENZR_LOG(kInfo, INFO)

define TENZR_LOG_WARN

#define TENZR_LOG_WARN TENZR_LOG(kWarn, WARN)

define TENZR_LOG_ERROR

#define TENZR_LOG_ERROR TENZR_LOG(kError, ERROR)

Source code

// Copyright 2026 The Tenzr Project Authors
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef TENZR_COMMON_LOGGING_H_
#define TENZR_COMMON_LOGGING_H_

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string_view>

namespace tenzr {

  enum class LogLevel { kDebug = 0, kInfo = 1, kWarn = 2, kError = 3 };

  inline LogLevel GetInitialLogLevel() {
    const char* env = std::getenv("TENZR_LOG_LEVEL");
    if (!env) {
      return LogLevel::kWarn;
    }

    std::string_view s(env);
    if (s == "DEBUG" || s == "debug" || s == "0") {
      return LogLevel::kDebug;
    }
    if (s == "INFO" || s == "info" || s == "1") {
      return LogLevel::kInfo;
    }
    if (s == "WARN" || s == "warn" || s == "WARNING" || s == "warning" || s == "2") {
      return LogLevel::kWarn;
    }
    if (s == "ERROR" || s == "error" || s == "3") {
      return LogLevel::kError;
    }
    return LogLevel::kWarn;
  }

  inline LogLevel& CurrentLogLevel() {
    static LogLevel level = GetInitialLogLevel();
    return level;
  }

  inline void SetLogLevel(LogLevel level) { CurrentLogLevel() = level; }

#define TENZR_LOG(lvl, prefix) \
  if (::tenzr::LogLevel::lvl >= ::tenzr::CurrentLogLevel()) std::cerr << "[" #prefix "] [" << __FILE__ << ":" << __LINE__ << "] "

#define TENZR_LOG_DEBUG TENZR_LOG(kDebug, DEBUG)
#define TENZR_LOG_INFO TENZR_LOG(kInfo, INFO)
#define TENZR_LOG_WARN TENZR_LOG(kWarn, WARN)
#define TENZR_LOG_ERROR TENZR_LOG(kError, ERROR)

}  // namespace tenzr

#endif  // TENZR_COMMON_LOGGING_H_