| 1 | /* | 
| 2 |   * Copyright 2010-2017 Amazon.com, Inc. or its affiliates. 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 |   * A copy of the License is located at | 
| 7 |   *  | 
| 8 |   *  http://aws.amazon.com/apache2.0 | 
| 9 |   *  | 
| 10 |   * or in the "license" file accompanying this file. This file is distributed | 
| 11 |   * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | 
| 12 |   * express or implied. See the License for the specific language governing | 
| 13 |   * permissions and limitations under the License. | 
| 14 |   */ | 
| 15 |  | 
| 16 |  | 
| 17 | #include <aws/core/utils/logging/FormattedLogSystem.h> | 
| 18 |  | 
| 19 | #include <aws/core/utils/DateTime.h> | 
| 20 | #include <aws/core/utils/Array.h> | 
| 21 |  | 
| 22 | #include <fstream> | 
| 23 | #include <cstdarg> | 
| 24 | #include <stdio.h> | 
| 25 | #include <thread> | 
| 26 |  | 
| 27 | using namespace Aws::Utils; | 
| 28 | using namespace Aws::Utils::Logging; | 
| 29 |  | 
| 30 | static Aws::String CreateLogPrefixLine(LogLevel logLevel, const char* tag) | 
| 31 | { | 
| 32 |     Aws::StringStream ss; | 
| 33 |  | 
| 34 |     switch(logLevel) | 
| 35 |     { | 
| 36 |         case LogLevel::Error: | 
| 37 |             ss << "[ERROR] " ; | 
| 38 |             break; | 
| 39 |  | 
| 40 |         case LogLevel::Fatal: | 
| 41 |             ss << "[FATAL] " ; | 
| 42 |             break; | 
| 43 |  | 
| 44 |         case LogLevel::Warn: | 
| 45 |             ss << "[WARN] " ; | 
| 46 |             break; | 
| 47 |  | 
| 48 |         case LogLevel::Info: | 
| 49 |             ss << "[INFO] " ; | 
| 50 |             break; | 
| 51 |  | 
| 52 |         case LogLevel::Debug: | 
| 53 |             ss << "[DEBUG] " ; | 
| 54 |             break; | 
| 55 |  | 
| 56 |         case LogLevel::Trace: | 
| 57 |             ss << "[TRACE] " ; | 
| 58 |             break; | 
| 59 |  | 
| 60 |         default: | 
| 61 |             ss << "[UNKOWN] " ; | 
| 62 |             break; | 
| 63 |     } | 
| 64 |  | 
| 65 |     ss << DateTime::Now().CalculateGmtTimeWithMsPrecision() << " "  << tag << " ["  << std::this_thread::get_id() << "] " ; | 
| 66 |  | 
| 67 |     return ss.str(); | 
| 68 | } | 
| 69 |  | 
| 70 | FormattedLogSystem::FormattedLogSystem(LogLevel logLevel) : | 
| 71 |     m_logLevel(logLevel) | 
| 72 | { | 
| 73 | } | 
| 74 |  | 
| 75 | void FormattedLogSystem::Log(LogLevel logLevel, const char* tag, const char* formatStr, ...) | 
| 76 | { | 
| 77 |     Aws::StringStream ss; | 
| 78 |     ss << CreateLogPrefixLine(logLevel, tag); | 
| 79 |  | 
| 80 |     std::va_list args; | 
| 81 |     va_start(args, formatStr); | 
| 82 |  | 
| 83 |     va_list tmp_args; //unfortunately you cannot consume a va_list twice | 
| 84 |     va_copy(tmp_args, args); //so we have to copy it | 
| 85 |     #ifdef WIN32 | 
| 86 |         const int requiredLength = _vscprintf(formatStr, tmp_args) + 1; | 
| 87 |     #else | 
| 88 |         const int requiredLength = vsnprintf(nullptr, 0, formatStr, tmp_args) + 1; | 
| 89 |     #endif | 
| 90 |     va_end(tmp_args); | 
| 91 |  | 
| 92 |     Array<char> outputBuff(requiredLength); | 
| 93 |     #ifdef WIN32 | 
| 94 |         vsnprintf_s(outputBuff.GetUnderlyingData(), requiredLength, _TRUNCATE, formatStr, args); | 
| 95 |     #else | 
| 96 |         vsnprintf(outputBuff.GetUnderlyingData(), requiredLength, formatStr, args); | 
| 97 |     #endif // WIN32 | 
| 98 |  | 
| 99 |     ss << outputBuff.GetUnderlyingData() << std::endl;   | 
| 100 |    | 
| 101 |     ProcessFormattedStatement(ss.str()); | 
| 102 |  | 
| 103 |     va_end(args); | 
| 104 | } | 
| 105 |  | 
| 106 | void FormattedLogSystem::LogStream(LogLevel logLevel, const char* tag, const Aws::OStringStream &message_stream) | 
| 107 | { | 
| 108 |     ProcessFormattedStatement(CreateLogPrefixLine(logLevel, tag) + message_stream.str() + "\n" ); | 
| 109 | } | 
| 110 |  |