1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2004-2021 musikcube team
4 //
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 //
10 //    * Redistributions of source code must retain the above copyright notice,
11 //      this list of conditions and the following disclaimer.
12 //
13 //    * Redistributions in binary form must reproduce the above copyright
14 //      notice, this list of conditions and the following disclaimer in the
15 //      documentation and/or other materials provided with the distribution.
16 //
17 //    * Neither the name of the author nor the names of other contributors may
18 //      be used to endorse or promote products derived from this software
19 //      without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 // POSSIBILITY OF SUCH DAMAGE.
32 //
33 //////////////////////////////////////////////////////////////////////////////
34 
35 #pragma once
36 
37 #include <string>
38 #include <vector>
39 #include <fstream>
40 #include <memory>
41 
42 namespace musik {
43     class debug {
44         public:
45             class IBackend {
46                 public:
~IBackend()47                     virtual ~IBackend() { }
48                     virtual void verbose(const std::string& tag, const std::string& string) = 0;
49                     virtual void info(const std::string& tag, const std::string& string) = 0;
50                     virtual void warning(const std::string& tag, const std::string& string) = 0;
51                     virtual void error(const std::string& tag, const std::string& string) = 0;
52             };
53 
54             class FileBackend : public IBackend {
55                 public:
56                     FileBackend(const std::string& fn);
57                     FileBackend(FileBackend&& fn);
58                     virtual ~FileBackend() override;
59                     virtual void verbose(const std::string& tag, const std::string& string) override;
60                     virtual void info(const std::string& tag, const std::string& string) override;
61                     virtual void warning(const std::string& tag, const std::string& string) override;
62                     virtual void error(const std::string& tag, const std::string& string) override;
63                 private:
64                     std::ofstream out;
65             };
66 
67             class SimpleFileBackend: public FileBackend {
68                 public:
69                     SimpleFileBackend();
70                     SimpleFileBackend(const std::string& fn) = delete;
71                     SimpleFileBackend(FileBackend&& fn) = delete;
72             };
73 
74             class ConsoleBackend : public IBackend {
75                 public:
76                     ConsoleBackend();
77                     virtual ~ConsoleBackend() override;
78                     virtual void verbose(const std::string& tag, const std::string& string) override;
79                     virtual void info(const std::string& tag, const std::string& string) override;
80                     virtual void warning(const std::string& tag, const std::string& string) override;
81                     virtual void error(const std::string& tag, const std::string& string) override;
82             };
83 
84             static void Start(std::vector<IBackend*> backends = { new SimpleFileBackend() });
85             static void Stop();
86 
87             static void verbose(const std::string& tag, const std::string& string) noexcept;
88             static void v(const std::string& tag, const std::string& string) noexcept;
89             static void info(const std::string& tag, const std::string& string) noexcept;
90             static void i(const std::string& tag, const std::string& string) noexcept;
91             static void warning(const std::string& tag, const std::string& string) noexcept;
92             static void w(const std::string& tag, const std::string& string) noexcept;
93             static void error(const std::string& tag, const std::string& string) noexcept;
94             static void e(const std::string& tag, const std::string& string) noexcept;
95     };
96 }
97