"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/common/Logger.cpp" between
ssr-0.4.0.tar.gz and ssr-0.4.1.tar.gz

About: SimpleScreenRecorder is a screen (video-audio) recorder for Linux.

Logger.cpp  (ssr-0.4.0):Logger.cpp  (ssr-0.4.1)
skipping to change at line 22 skipping to change at line 22
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with SimpleScreenRecorder. If not, see <http://www.gnu.org/licenses/>. along with SimpleScreenRecorder. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "Logger.h" #include "Logger.h"
#include "QueueBuffer.h"
Logger *Logger::s_instance = NULL; Logger *Logger::s_instance = NULL;
static QString LogFormatTime() {
return QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
}
Logger::Logger() { Logger::Logger() {
assert(s_instance == NULL); assert(s_instance == NULL);
qRegisterMetaType<enum_type>(); qRegisterMetaType<enum_type>();
m_capture_pipe[0] = -1;
m_capture_pipe[1] = -1;
m_original_stderr = -1;
try {
Init();
} catch(...) {
Free();
throw;
}
s_instance = this; s_instance = this;
} }
Logger::~Logger() { Logger::~Logger() {
assert(s_instance == this); assert(s_instance == this);
s_instance = NULL; s_instance = NULL;
Free();
}
void Logger::SetLogFile(const QString &filename) {
m_log_file.setFileName(filename);
m_log_file.open(QFile::WriteOnly | QFile::Append | QFile::Text | QFile::U
nbuffered);
} }
void Logger::LogInfo(const QString& str) { void Logger::LogInfo(const QString& str) {
assert(s_instance != NULL); assert(s_instance != NULL);
std::lock_guard<std::mutex> lock(s_instance->m_mutex); Q_UNUSED(lock); std::lock_guard<std::mutex> lock(s_instance->m_mutex); Q_UNUSED(lock);
fprintf(stderr, "%s\n", str.toLocal8Bit().constData()); QByteArray buf = (str + "\n").toLocal8Bit();
write(s_instance->m_original_stderr, buf.constData(), buf.size());
if(s_instance->m_log_file.isOpen())
s_instance->m_log_file.write((LogFormatTime() + " (I) " + str + "
\n").toLocal8Bit());
emit s_instance->NewLine(TYPE_INFO, str); emit s_instance->NewLine(TYPE_INFO, str);
} }
void Logger::LogWarning(const QString& str) { void Logger::LogWarning(const QString& str) {
assert(s_instance != NULL); assert(s_instance != NULL);
std::lock_guard<std::mutex> lock(s_instance->m_mutex); Q_UNUSED(lock); std::lock_guard<std::mutex> lock(s_instance->m_mutex); Q_UNUSED(lock);
fprintf(stderr, "\033[1;33m%s\033[0m\n", str.toLocal8Bit().constData()); QByteArray buf = ("\033[1;33m" + str + "\033[0m\n").toLocal8Bit();
write(s_instance->m_original_stderr, buf.constData(), buf.size());
if(s_instance->m_log_file.isOpen())
s_instance->m_log_file.write((LogFormatTime() + " (W) " + str + "
\n").toLocal8Bit());
emit s_instance->NewLine(TYPE_WARNING, str); emit s_instance->NewLine(TYPE_WARNING, str);
} }
void Logger::LogError(const QString& str) { void Logger::LogError(const QString& str) {
assert(s_instance != NULL); assert(s_instance != NULL);
std::lock_guard<std::mutex> lock(s_instance->m_mutex); Q_UNUSED(lock); std::lock_guard<std::mutex> lock(s_instance->m_mutex); Q_UNUSED(lock);
fprintf(stderr, "\033[1;31m%s\033[0m\n", str.toLocal8Bit().constData()); QByteArray buf = ("\033[1;31m" + str + "\033[0m\n").toLocal8Bit();
write(s_instance->m_original_stderr, buf.constData(), buf.size());
if(s_instance->m_log_file.isOpen())
s_instance->m_log_file.write((LogFormatTime() + " (E) " + str + "
\n").toLocal8Bit());
emit s_instance->NewLine(TYPE_ERROR, str); emit s_instance->NewLine(TYPE_ERROR, str);
} }
void Logger::Init() {
if(pipe2(m_capture_pipe, O_CLOEXEC) != 0)
throw std::runtime_error("Failed to create capture pipe");
m_original_stderr = dup(2); // copy stderr
dup2(m_capture_pipe[1], 2); // redirect stderr
m_capture_thread = std::thread(&Logger::CaptureThread, this);
}
void Logger::Free() {
if(m_original_stderr != -1) {
dup2(m_original_stderr, 2); // restore stderr
}
if(m_capture_pipe[1] != -1) {
close(m_capture_pipe[1]); // close write end of pipe
m_capture_pipe[1] = -1;
}
if(m_capture_thread.joinable()) {
m_capture_thread.join(); // wait for thread
}
if(m_capture_pipe[0] != -1) {
close(m_capture_pipe[0]); // close read end of pipe
m_capture_pipe[0] = -1;
}
if(m_original_stderr != -1) {
close(m_original_stderr); // close copy of stderr
m_original_stderr = -1;
}
}
void Logger::CaptureThread() {
QueueBuffer<char> buffer;
size_t pos = 0;
for( ; ; ) {
ssize_t num;
do {
num = read(m_capture_pipe[0], buffer.Reserve(PIPE_BUF), P
IPE_BUF);
} while(num == -1 && errno == EINTR);
if(num <= 0)
break;
buffer.Push(num);
while(pos < buffer.GetSize()) {
if(buffer[pos] == '\n') {
std::lock_guard<std::mutex> lock(s_instance->m_mu
tex); Q_UNUSED(lock);
std::string buf = "\033[2m" + std::string(buffer.
GetData(), pos) + "\033[0m\n";
write(s_instance->m_original_stderr, buf.data(),
buf.size());
QString str = QString::fromLocal8Bit(buffer.GetDa
ta(), pos);
if(s_instance->m_log_file.isOpen())
s_instance->m_log_file.write((LogFormatTi
me() + " (S) " + str + "\n").toLocal8Bit());
emit s_instance->NewLine(TYPE_STDERR, str);
buffer.Pop(pos + 1);
pos = 0;
} else {
++pos;
}
}
}
}
 End of changes. 8 change blocks. 
3 lines changed or deleted 37 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)