olp-cpp-sdk  1.22.0
Format.h
1 /*
2  * Copyright (C) 2019 HERE Europe B.V.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  * License-Filename: LICENSE
18  */
19 
20 #pragma once
21 
22 #include <olp/core/CoreApi.h>
23 
24 #include <chrono>
25 #include <cstdarg>
26 #include <string>
27 #include <vector>
28 
29 #if (defined(__GNUC__) || defined(__clang__))
30 #define CHECK_PRINTF_FORMAT_STRING(format_string_pos, first_to_check) \
31  __attribute__((format(printf, (format_string_pos), (first_to_check))))
32 #else
33 #define CHECK_PRINTF_FORMAT_STRING(format_string_pos, first_to_check)
34 #endif
35 
36 namespace olp {
37 namespace logging {
41 using TimePoint = std::chrono::time_point<std::chrono::system_clock>;
42 
50 CORE_API std::string format(const char* formatStr, ...)
51  CHECK_PRINTF_FORMAT_STRING(1, 2);
52 
62 CORE_API std::string formatv(const char* formatStr, va_list args);
63 
74 CORE_API std::string formatLocalTime(const TimePoint& timestamp);
75 
84 CORE_API std::string formatLocalTime(const TimePoint& timestamp,
85  const char* formatStr);
86 
97 CORE_API std::string formatUtcTime(const TimePoint& timestamp);
98 
107 CORE_API std::string formatUtcTime(const TimePoint& timestamp,
108  const char* formatStr);
109 
116 class CORE_API FormatBuffer {
117  public:
118  FormatBuffer() = default;
119 
127  const char* format(const char* formatStr, ...)
128  CHECK_PRINTF_FORMAT_STRING(2, 3);
129 
139  const char* formatv(const char* formatStr, va_list args);
140 
151  const char* formatLocalTime(const TimePoint& timestamp);
152 
161  const char* formatLocalTime(const TimePoint& timestamp,
162  const char* formatStr);
163 
174  const char* formatUtcTime(const TimePoint& timestamp);
175 
184  const char* formatUtcTime(const TimePoint& timestamp, const char* formatStr);
185 
186  private:
187  friend std::string logging::formatv(const char* formatStr, va_list args);
188 
189  const char* formatTm(const struct tm& timestampTm, const char* formatStr);
190 
191  static const unsigned int bufferSize = 256;
192  char m_buffer[bufferSize]{};
193  std::vector<char> m_auxBuffer;
194 };
195 
196 } // namespace logging
197 } // namespace olp
Attempts to format a string to a buffer before falling back to a dynamically allocated string.
Definition: Format.h:116
const char * format(const char *formatStr,...) CHECK_PRINTF_FORMAT_STRING(2
Formats a string using a printf-style format string.
Rules all the other namespaces.
Definition: AppleSignInProperties.h:24