winlin

Merge pull request #28 from wenjiegit/master

merge from wenjie.zhao for colorful log.
... ... @@ -308,7 +308,7 @@ int SrsConfDirective::read_token(SrsFileBuffer* buffer, std::vector<string>& arg
srs_error("line %d: unexpected end of file, expecting ; or \"}\"", buffer->line);
return ERROR_SYSTEM_CONFIG_INVALID;
}
srs_error("end of file. ret=%d", ret);
srs_trace("end of file. ret=%d", ret);
return ret;
}
... ...
... ... @@ -81,7 +81,7 @@ void SrsFastLog::verbose(const char* tag, int context_id, const char* fmt, ...)
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap);
write_log(log_data, size);
write_log(log_data, size, SrsLogLevel::Verbose);
}
void SrsFastLog::info(const char* tag, int context_id, const char* fmt, ...)
... ... @@ -101,7 +101,7 @@ void SrsFastLog::info(const char* tag, int context_id, const char* fmt, ...)
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap);
write_log(log_data, size);
write_log(log_data, size, SrsLogLevel::Info);
}
void SrsFastLog::trace(const char* tag, int context_id, const char* fmt, ...)
... ... @@ -121,7 +121,7 @@ void SrsFastLog::trace(const char* tag, int context_id, const char* fmt, ...)
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap);
write_log(log_data, size);
write_log(log_data, size, SrsLogLevel::Trace);
}
void SrsFastLog::warn(const char* tag, int context_id, const char* fmt, ...)
... ... @@ -141,7 +141,7 @@ void SrsFastLog::warn(const char* tag, int context_id, const char* fmt, ...)
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap);
write_log(log_data, size);
write_log(log_data, size, SrsLogLevel::Warn);
}
void SrsFastLog::error(const char* tag, int context_id, const char* fmt, ...)
... ... @@ -161,7 +161,10 @@ void SrsFastLog::error(const char* tag, int context_id, const char* fmt, ...)
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap);
write_log(log_data, size);
// add strerror() to error msg.
size += snprintf(log_data + size, LOG_MAX_SIZE - size, "(%s)", strerror(errno));
write_log(log_data, size, SrsLogLevel::Error);
}
bool SrsFastLog::generate_header(const char* tag, int context_id, const char* level_name, int* header_size)
... ... @@ -203,7 +206,7 @@ bool SrsFastLog::generate_header(const char* tag, int context_id, const char* le
return true;
}
void SrsFastLog::write_log(char* str_log, int size)
void SrsFastLog::write_log(char *str_log, int size, int _level)
{
// ensure the tail and EOF of string
// LOG_TAIL_SIZE for the TAIL char.
... ... @@ -214,5 +217,12 @@ void SrsFastLog::write_log(char* str_log, int size)
log_data[size++] = LOG_TAIL;
log_data[size++] = 0;
printf("%s", str_log);
// if is error msg, then print color msg.
// \033[1;31m : red text code in shell
// \033[1;31m : normal text code
if (_level == SrsLogLevel::Error) {
printf("\033[1;31m%s\033[0m", str_log);
} else {
printf("%s", str_log);
}
}
... ...
... ... @@ -91,7 +91,7 @@ public:
virtual void error(const char* tag, int context_id, const char* fmt, ...);
private:
virtual bool generate_header(const char* tag, int context_id, const char* level_name, int* header_size);
virtual void write_log(char* str_log, int size);
virtual void write_log(char* str_log, int size, int _level);
};
#endif
... ...