1 #include "utils/PVLog.hpp" 15 LogFileStream(std::basic_ostream<T> &st) : mDefaultStream(st) { setStreamDefault(); }
17 if (mCreatedWithNew) {
22 std::basic_ostream<T> &getStream() {
return *mStream; }
23 void setStream(
char const *path, std::ios_base::openmode mode = std::ios_base::out) {
24 if (mCreatedWithNew) {
28 char *realPath = strdup(expandLeadingTilde(path).c_str());
29 if (realPath ==
nullptr) {
31 std::cerr <<
"LogFileStream::setStream failed for \"" << realPath <<
"\"\n";
34 mStream =
new std::basic_ofstream<T>(realPath, mode);
35 if (mStream->fail() || mStream->fail()) {
37 std::cerr <<
"Unable to open logFile \"" << path <<
"\"." << std::endl;
40 mCreatedWithNew =
true;
44 mStream = &mDefaultStream;
45 mCreatedWithNew =
false;
48 void setStream(std::basic_ostream<T> *stream) {
49 if (mCreatedWithNew) {
53 mCreatedWithNew =
false;
55 void setStreamDefault() {
56 if (mCreatedWithNew) {
59 mStream = &mDefaultStream;
60 mCreatedWithNew =
false;
64 std::basic_ostream<T> &mDefaultStream;
65 std::basic_ostream<T> *mStream =
nullptr;
66 bool mCreatedWithNew =
false;
74 std::ostream &getErrorStream() {
return errorLogFileStream.getStream(); }
75 std::ostream &getOutputStream() {
return outputLogFileStream.getStream(); }
77 std::wostream &getWErrorStream() {
return errorLogFileWStream.getStream(); }
78 std::wostream &getWOutputStream() {
return outputLogFileWStream.getStream(); }
80 void setLogFile(std::string
const &logFile, std::ios_base::openmode mode) {
81 if (!logFile.empty()) {
82 setLogFile(logFile.c_str(), mode);
86 void setLogFile(
char const *logFile, std::ios_base::openmode mode) {
87 outputLogFileStream.setStream(logFile, mode);
89 errorLogFileStream.setStream(&getOutputStream());
92 errorLogFileStream.setStream(logFile, mode);
96 void setWLogFile(std::wstring
const &logFile, std::ios_base::openmode mode) {
97 if (!logFile.empty()) {
98 setWLogFile(logFile.c_str(), mode);
102 void setWLogFile(
char const *logFile, std::ios_base::openmode mode) {
103 outputLogFileWStream.setStream(logFile, mode);
105 errorLogFileWStream.setStream(&getWOutputStream());
108 errorLogFileWStream.setStream(logFile, mode);