40 std::map<RestFrames::LogType,bool> RFLog::m_PrintMap = InitPrintMap();
41 std::ostream* RFLog::m_Ostr = &std::cerr;
42 bool RFLog::m_Color =
true;
43 int RFLog::m_NMAX = 100;
45 RFLog::RFLog(
const std::string& source, LogType def_type)
56 RFLog g_Log(
"RestFrames Global");
62 m_CurType = m_DefType;
64 m_TypeMap[LogVerbose] =
"VERBOSE";
65 m_TypeMap[LogDebug] =
"DEBUG";
66 m_TypeMap[LogInfo] =
"INFO";
67 m_TypeMap[LogWarning] =
"WARNING";
68 m_TypeMap[LogError] =
"ERROR";
70 m_ColorMap[LogVerbose] =
"\x1b[36m";
71 m_ColorMap[LogDebug] =
"\x1b[33m";
72 m_ColorMap[LogInfo] =
"\x1b[32m";
73 m_ColorMap[LogWarning] =
"\x1b[35m";
74 m_ColorMap[LogError] =
"\x1b[31m";
77 std::map<LogType,bool> InitPrintMap(){
78 std::map<LogType,bool> m;
79 m[LogVerbose] =
false;
87 std::string RFLog::GetFormattedSource()
const {
88 std::string source_name = m_Source;
89 if (source_name.size() > 22){
90 source_name = source_name.substr( 0, 22 - 3 );
96 std::string RFLog::GetFormattedMessage(
const std::string& message) {
97 std::string output =
"";
98 int N = message.size();
101 int Ncut = (N-OFF)/m_NMAX;
102 if((N-OFF)%m_NMAX == 0)
104 std::string::size_type previous_pos = 0;
105 for(
int i = 0; i <= Ncut; i++){
107 if(i == 0) off += OFF;
108 std::string line = message.substr(previous_pos, off);
111 output += m_ColorMap[m_CurType]+
"<...>\x1b[0m ...";
113 output +=
"<...> ...";
117 if(
int(previous_pos) != N && i != Ncut) output +=
"...\n";
126 std::string source_name = GetFormattedSource();
127 std::string message = m_Message.str();
128 std::string::size_type previous_pos = 0, current_pos = 0;
129 if(m_PrintMap[m_CurType] && m_Ostr){
132 prefix = m_ColorMap[m_CurType]+
"<"+m_TypeMap[m_CurType]+
">";
134 prefix =
"<"+m_TypeMap[m_CurType]+
">";
135 for(
int i = 0; i < 8-int(m_TypeMap[m_CurType].size()); i++){
138 prefix += source_name+
": ";
142 current_pos = message.find(
'\n', previous_pos );
143 std::string line = message.substr( previous_pos, current_pos - previous_pos );
145 std::ostringstream message_to_send;
146 message_to_send.setf(std::ios::adjustfield, std::ios::left);
147 line = GetFormattedMessage(prefix+line);
148 message_to_send << line << std::endl;
150 *m_Ostr << message_to_send.str();
153 if (current_pos == message.npos)
break;
154 previous_pos = current_pos + 1;
158 if (m_CurType == LogError)
159 throw RestFramesException(m_Message.str());
162 m_CurType = m_DefType;
166 RFLog& RFLog::EndMessage(RFLog& log){
171 void RFLog::PrintObject(
const RFBase* objPtr){
172 m_Message << objPtr->PrintString(LogVerbose);
176 void RFLog::PrintList(
const RFList<T>* listPtr){
177 int N = listPtr->GetN();
178 for(
int i = 0; i < N; i++)
179 m_Message << listPtr->Get(i).GetName() <<
" ";
182 void RFLog::SetSource(
const std::string& source){
186 void SetLogPrint(LogType type,
bool print){
187 RFLog::m_PrintMap[type] = print;
190 void SetLogPrint(
bool print){
191 for (std::map<LogType, bool>::iterator m = RFLog::m_PrintMap.begin();
192 m != RFLog::m_PrintMap.end(); ++m)
193 m->second = (m->second && print);
196 void SetLogStream(std::ostream* ostr){
197 if(ostr) RFLog::m_Ostr = ostr;
200 void SetLogColor(
bool color){
201 RFLog::m_Color = color;
204 void SetLogMaxWidth(
int NMAX){
205 if(NMAX > 0) RFLog::m_NMAX = NMAX;
208 template <> RFLog& RFLog::operator << (
const RFBase* arg){
213 template <> RFLog& RFLog::operator << (
const RFBaseList* arg){
218 const RFBase* Log(
const RFBase& obj){
return (
const RFBase*)&obj; }
219 const RFBase* Log(
const RFBase* ptr){
return (
const RFBase*)ptr; }