Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Source/CPack/cmCPackLog.h
4998 views
1
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2
file LICENSE.rst or https://cmake.org/licensing for details. */
3
#pragma once
4
5
#include "cmConfigure.h" // IWYU pragma: keep
6
7
#include <cstring>
8
#include <memory>
9
#include <ostream>
10
#include <string>
11
12
#define cmCPack_Log(ctSelf, logType, msg) \
13
do { \
14
std::ostringstream cmCPackLog_msg; \
15
cmCPackLog_msg << msg; \
16
(ctSelf)->Log(logType, __FILE__, __LINE__, cmCPackLog_msg.str().c_str()); \
17
} while (false)
18
19
/** \class cmCPackLog
20
* \brief A container for CPack generators
21
*
22
*/
23
class cmCPackLog
24
{
25
public:
26
cmCPackLog();
27
~cmCPackLog();
28
29
cmCPackLog(cmCPackLog const&) = delete;
30
cmCPackLog& operator=(cmCPackLog const&) = delete;
31
32
enum cm_log_tags
33
{
34
NOTAG = 0,
35
LOG_OUTPUT = 0x1,
36
LOG_VERBOSE = 0x2,
37
LOG_DEBUG = 0x4,
38
LOG_WARNING = 0x8,
39
LOG_ERROR = 0x10
40
};
41
42
//! Various signatures for logging.
43
void Log(char const* file, int line, char const* msg)
44
{
45
this->Log(LOG_OUTPUT, file, line, msg);
46
}
47
void Log(char const* file, int line, char const* msg, size_t length)
48
{
49
this->Log(LOG_OUTPUT, file, line, msg, length);
50
}
51
void Log(int tag, char const* file, int line, char const* msg)
52
{
53
this->Log(tag, file, line, msg, strlen(msg));
54
}
55
void Log(int tag, char const* file, int line, char const* msg,
56
size_t length);
57
58
//! Set Verbose
59
void VerboseOn() { this->SetVerbose(true); }
60
void VerboseOff() { this->SetVerbose(true); }
61
void SetVerbose(bool verb) { this->Verbose = verb; }
62
bool GetVerbose() { return this->Verbose; }
63
64
//! Set Debug
65
void DebugOn() { this->SetDebug(true); }
66
void DebugOff() { this->SetDebug(true); }
67
void SetDebug(bool verb) { this->Debug = verb; }
68
bool GetDebug() { return this->Debug; }
69
70
//! Set Quiet
71
void QuietOn() { this->SetQuiet(true); }
72
void QuietOff() { this->SetQuiet(true); }
73
void SetQuiet(bool verb) { this->Quiet = verb; }
74
bool GetQuiet() { return this->Quiet; }
75
76
//! Set the output stream
77
void SetOutputStream(std::ostream* os) { this->DefaultOutput = os; }
78
79
//! Set the error stream
80
void SetErrorStream(std::ostream* os) { this->DefaultError = os; }
81
82
//! Set the log output stream
83
void SetLogOutputStream(std::ostream* os);
84
85
//! Set the log output file. The cmCPackLog will try to create file. If it
86
// cannot, it will report an error.
87
bool SetLogOutputFile(char const* fname);
88
89
//! Set the various prefixes for the logging. SetPrefix sets the generic
90
// prefix that overwrites missing ones.
91
void SetPrefix(std::string const& pfx) { this->Prefix = pfx; }
92
void SetOutputPrefix(std::string const& pfx) { this->OutputPrefix = pfx; }
93
void SetVerbosePrefix(std::string const& pfx) { this->VerbosePrefix = pfx; }
94
void SetDebugPrefix(std::string const& pfx) { this->DebugPrefix = pfx; }
95
void SetWarningPrefix(std::string const& pfx) { this->WarningPrefix = pfx; }
96
void SetErrorPrefix(std::string const& pfx) { this->ErrorPrefix = pfx; }
97
98
private:
99
bool Verbose = false;
100
bool Debug = false;
101
bool Quiet = false;
102
103
bool NewLine = true;
104
105
int LastTag = cmCPackLog::NOTAG;
106
107
std::string Prefix;
108
std::string OutputPrefix;
109
std::string VerbosePrefix;
110
std::string DebugPrefix;
111
std::string WarningPrefix;
112
std::string ErrorPrefix;
113
114
std::ostream* DefaultOutput = nullptr;
115
std::ostream* DefaultError = nullptr;
116
117
std::ostream* LogOutput = nullptr;
118
std::unique_ptr<std::ostream> LogOutputStream;
119
};
120
121
class cmCPackLogWrite
122
{
123
public:
124
cmCPackLogWrite(char const* data, size_t length)
125
: Data(data)
126
, Length(length)
127
{
128
}
129
130
char const* Data;
131
std::streamsize Length;
132
};
133
134
inline std::ostream& operator<<(std::ostream& os, cmCPackLogWrite const& c)
135
{
136
os.write(c.Data, c.Length);
137
os.flush();
138
return os;
139
}
140
141