1 /*
2 * Copyright 2009-2017 Alibaba Cloud All rights reserved.
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
17 #include "Utils.h"
18 #include "LogUtils.h"
19 #include <iostream>
20 #include <memory>
21 #include <cstdarg>
22 #include <sstream>
23 #include <chrono>
24 #include <iomanip>
25 #include <ctime>
26 #include <thread>
27 #include <cstdarg>
28 using namespace AlibabaCloud::OSS;
29
30 static LogLevel gOssLogLevel = LogLevel::LogAll;
31 static LogCallback gLogCallback = nullptr;
32 const static char *EnvLogLevels[] =
33 {
34 "off", "fatal", "error", "warn",
35 "info", "debug", "trace", "all"
36 };
37
LogPrefix(LogLevel logLevel,const char * tag)38 static std::string LogPrefix(LogLevel logLevel, const char* tag)
39 {
40 static const char *LogStr[] = {"[OFF]", "[FATAL]", "[ERROR]", "[WARN]", "[INFO]" , "[DEBUG]", "[TRACE]", "[ALL]"};
41 int index = logLevel - LogLevel::LogOff;
42 std::stringstream ss;
43 auto tp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
44 auto ms = tp.time_since_epoch().count() % 1000;
45 auto t = std::chrono::system_clock::to_time_t(tp);
46 struct tm tm;
47 #ifdef WIN32
48 ::localtime_s(&tm, &t);
49 #else
50 ::localtime_r(&t, &tm);
51 #endif
52 #if defined(__GNUG__) && __GNUC__ < 5
53 char tmbuff[64];
54 strftime(tmbuff, 64, "%Y-%m-%d %H:%M:%S.", &tm);
55 ss << "[" << tmbuff << std::setw(3) << std::setfill('0') << ms << "]";
56 #else
57 ss << "[" << std::put_time(&tm, "%Y-%m-%d %H:%M:%S.") << std::setw(3) << std::setfill('0') << ms << "]";
58 #endif
59 ss << LogStr[index];
60 ss << "[" << tag << "]";
61 ss << "[" << std::this_thread::get_id() << "]";
62 return ss.str();
63 }
64
FormattedLog(LogLevel logLevel,const char * tag,const char * fmt,...)65 void AlibabaCloud::OSS::FormattedLog(LogLevel logLevel, const char* tag, const char* fmt, ...)
66 {
67 std::stringstream ss;
68 ss << LogPrefix(logLevel, tag);
69 char buffer[2050];
70 int i = 0;
71 va_list args;
72 va_start(args, fmt);
73 #ifdef WIN32
74 i = vsnprintf_s(buffer, sizeof(buffer) - 1, _TRUNCATE, fmt, args);
75 #else
76 i = vsnprintf(buffer, sizeof(buffer) - 1, fmt, args);
77 #endif
78 va_end(args);
79
80 while (i > 0 && buffer[i - 1] == '\n') {
81 i--;
82 buffer[i] = '\0';
83 }
84
85 std::cout << buffer << std::endl;
86 if (gLogCallback) {
87 gLogCallback(logLevel, ss.str());
88 }
89 }
90
DefaultLogCallbackFunc(LogLevel level,const std::string & stream)91 static void DefaultLogCallbackFunc(LogLevel level, const std::string &stream)
92 {
93 UNUSED_PARAM(level);
94 std::cerr << stream;
95 }
96
GetLogLevelInner()97 LogLevel AlibabaCloud::OSS::GetLogLevelInner()
98 {
99 return gOssLogLevel;
100 }
101
GetLogCallbackInner()102 LogCallback AlibabaCloud::OSS::GetLogCallbackInner()
103 {
104 return gLogCallback;
105 }
106
SetLogLevelInner(LogLevel level)107 void AlibabaCloud::OSS::SetLogLevelInner(LogLevel level)
108 {
109 gOssLogLevel = level;
110 }
111
SetLogCallbackInner(LogCallback callback)112 void AlibabaCloud::OSS::SetLogCallbackInner(LogCallback callback)
113 {
114 gLogCallback = callback;
115 }
116
InitLogInner()117 void AlibabaCloud::OSS::InitLogInner()
118 {
119 gOssLogLevel = LogLevel::LogOff;
120 gLogCallback = nullptr;
121 auto value = std::getenv("OSS_SDK_LOG_LEVEL");
122 if (value) {
123 auto level = ToLower(Trim(value).c_str());
124 const auto size = sizeof(EnvLogLevels)/sizeof(EnvLogLevels[0]);
125 for (auto i = 0U; i < size; i++) {
126 if (level.compare(EnvLogLevels[i]) == 0) {
127 gOssLogLevel = static_cast<decltype(LogLevel::LogOff)>(static_cast<decltype(i)>(LogLevel::LogOff) + i);
128 gLogCallback = DefaultLogCallbackFunc;
129 break;
130 }
131 }
132 }
133 }
134
DeinitLogInner()135 void AlibabaCloud::OSS::DeinitLogInner()
136 {
137 gOssLogLevel = LogLevel::LogOff;
138 gLogCallback = nullptr;
139 }
140