File size: 3,389 Bytes
bfd87b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#pragma once

#include <aws/core/Core_EXPORTS.h>

#include <aws/core/utils/logging/FormattedLogSystem.h>
#include <aws/core/utils/logging/LogLevel.h>
#include <aws/core/utils/memory/stl/AWSVector.h>
#include <aws/core/utils/memory/stl/AWSString.h>
#include <aws/core/utils/memory/stl/AWSStreamFwd.h>

#include <thread>
#include <memory>
#include <mutex>
#include <atomic>
#include <condition_variable>

namespace Aws
{
    namespace Utils
    {
        namespace Logging
        {
            /**
             * Default behavior logger. It has a background thread that reads a log queue and prints the messages
             * out to file as quickly as possible.  This implementation also rolls the file every hour.
             */
            class AWS_CORE_API DefaultLogSystem : public FormattedLogSystem
            {
            public:
                using Base = FormattedLogSystem;

                /**
                 * Initialize the logging system to write to the supplied logfile output. Creates logging thread on construction.
                 */
                DefaultLogSystem(LogLevel logLevel, const std::shared_ptr<Aws::OStream>& logFile);
                /**
                 * Initialize the logging system to write to a computed file path filenamePrefix + "timestamp.log". Creates logging thread
                 * on construction.
                 */
                DefaultLogSystem(LogLevel logLevel, const Aws::String& filenamePrefix);

                virtual ~DefaultLogSystem();

                /**
                 * Flushes buffered messages to the file system.
                 * This method is thread-safe.
                 */
                void Flush() override;

                /**
                 * Stops logging on this logger without destroying the object.
                 */
                void Stop() override;

                /**
                 * Structure containing semaphores, queue etc... 
                 */
                struct LogSynchronizationData
                {
                public:
                    LogSynchronizationData() : m_stopLogging(false), m_loggingThreadStopped(false) {}

                    std::mutex m_logQueueMutex;
                    std::condition_variable m_queueSignal;
                    Aws::Vector<Aws::String> m_queuedLogMessages;
                    bool m_stopLogging = false;
                    bool m_loggingThreadStopped = false;

                private:
                    LogSynchronizationData(const LogSynchronizationData& rhs) = delete;
                    LogSynchronizationData& operator =(const LogSynchronizationData& rhs) = delete;
                };

            protected:
                /**
                 * Pushes log onto the queue and notifies the background thread.
                 */
                virtual void ProcessFormattedStatement(Aws::String&& statement) override;

            private:
                DefaultLogSystem(const DefaultLogSystem& rhs) = delete;
                DefaultLogSystem& operator =(const DefaultLogSystem& rhs) = delete;

                LogSynchronizationData m_syncData;

                std::thread m_loggingThread;
            };

        } // namespace Logging
    } // namespace Utils
} // namespace Aws