azure-core
Loading...
Searching...
No Matches
log.hpp
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4#pragma once
5
7#include "azure/core/dll_import_export.hpp"
8
9#include <atomic>
10#include <iostream>
11#include <mutex>
12#include <sstream>
13#include <type_traits>
14
15namespace Azure { namespace Core { namespace Diagnostics { namespace _internal {
16
57 class Log final {
58 static_assert(
59 std::is_same<int, std::underlying_type<Logger::Level>::type>::value == true,
60 "Logger::Level values must be representable as lock-free");
61
62 static_assert(ATOMIC_INT_LOCK_FREE == 2, "atomic<int> must be lock-free");
63
64 static_assert(ATOMIC_BOOL_LOCK_FREE == 2, "atomic<bool> must be lock-free");
65
66 static AZ_CORE_DLLEXPORT std::atomic<bool> g_isLoggingEnabled;
67 static AZ_CORE_DLLEXPORT std::atomic<Logger::Level> g_logLevel;
68
69 Log() = delete;
70 ~Log() = delete;
71
72 public:
93 class Stream final {
94 public:
99 Stream(Logger::Level level) : m_level{level} {}
101 ~Stream() { Log::Write(m_level, m_stream.str()); }
102 Stream(Stream const&) = delete;
103 Stream& operator=(Stream const&) = delete;
104
110 template <typename T> std::ostream& operator<<(T val) { return m_stream << val; }
111
112 private:
113 std::stringstream m_stream;
114 Logger::Level m_level;
115 };
116
125 static bool ShouldWrite(Logger::Level level)
126 {
127 return g_isLoggingEnabled && level >= g_logLevel;
128 }
129
149 static void Write(Logger::Level level, std::string const& message);
150
155 static void EnableLogging(bool isEnabled);
156
164 static void SetLogLevel(Logger::Level logLevel);
165
166 private:
167 };
168}}}} // namespace Azure::Core::Diagnostics::_internal
Level
Log message level.
Definition logger.hpp:26
Handling log messages from Azure SDK.
Compute the hash value for the input binary data, using SHA256, SHA384 and SHA512.
Definition azure_assert.hpp:57