2016-04-16 17:51:25 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-04-16 17:51:25 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-04-16 17:51:25 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QtCore/QString>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
namespace codegen {
|
|
|
|
namespace common {
|
|
|
|
|
2016-04-21 17:57:29 +00:00
|
|
|
// Common error codes.
|
|
|
|
constexpr int kErrorFileNotFound = 101;
|
|
|
|
constexpr int kErrorFileTooLarge = 102;
|
|
|
|
constexpr int kErrorFileNotOpened = 103;
|
|
|
|
constexpr int kErrorUnexpectedEndOfFile = 104;
|
|
|
|
|
2016-04-16 17:51:25 +00:00
|
|
|
// Wrapper around std::ostream that adds '\n' to the end of the logging line.
|
|
|
|
class LogStream {
|
|
|
|
public:
|
2016-04-17 13:22:00 +00:00
|
|
|
enum NullType {
|
|
|
|
Null,
|
|
|
|
};
|
|
|
|
explicit LogStream(NullType) : final_(false) {
|
|
|
|
}
|
|
|
|
explicit LogStream(std::ostream &stream) : stream_(&stream) {
|
2016-04-16 17:51:25 +00:00
|
|
|
}
|
2016-04-17 17:52:17 +00:00
|
|
|
LogStream(LogStream &&other) : stream_(other.stream_), final_(other.final_) {
|
2016-04-16 17:51:25 +00:00
|
|
|
other.final_ = false;
|
|
|
|
}
|
2016-04-17 13:22:00 +00:00
|
|
|
std::ostream *stream() const {
|
2016-04-16 17:51:25 +00:00
|
|
|
return stream_;
|
|
|
|
}
|
|
|
|
~LogStream() {
|
|
|
|
if (final_) {
|
2016-04-17 13:22:00 +00:00
|
|
|
*stream_ << '\n';
|
2016-04-16 17:51:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-04-17 13:22:00 +00:00
|
|
|
std::ostream *stream_ = nullptr;
|
2016-04-16 17:51:25 +00:00
|
|
|
bool final_ = true;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
LogStream operator<<(LogStream &&stream, T &&value) {
|
2016-04-17 13:22:00 +00:00
|
|
|
if (auto ostream = stream.stream()) {
|
|
|
|
*ostream << std::forward<T>(value);
|
|
|
|
}
|
2016-04-16 17:51:25 +00:00
|
|
|
return std::forward<LogStream>(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Outputs file name, line number and error code to std::err. Usage:
|
|
|
|
// logError(kErrorFileTooLarge, filepath) << "file too large, size=" << size;
|
|
|
|
LogStream logError(int code, const QString &filepath, int line = 0);
|
|
|
|
|
2016-09-23 16:04:26 +00:00
|
|
|
void logSetWorkingPath(const QString &workingpath);
|
|
|
|
|
2016-04-17 13:22:00 +00:00
|
|
|
static constexpr int kErrorInternal = 666;
|
|
|
|
|
2016-04-16 17:51:25 +00:00
|
|
|
} // namespace common
|
|
|
|
} // namespace codegen
|