boinc/lib/messages.C

146 lines
4.0 KiB
C++
Raw Normal View History

// The contents of this file are subject to the Mozilla Public License
// Version 1.0 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
// License for the specific language governing rights and limitations
// under the License.
//
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
//
// The Initial Developer of the Original Code is the SETI@home project.
// Portions created by the SETI@home project are Copyright (C) 2002, 2003
// University of California at Berkeley. All Rights Reserved.
//
// Contributor(s):
//
#include "util.h"
#include <cassert>
#include <cstring>
#include <string>
#include <fstream>
using namespace std;
Messages::Messages(FILE* output_)
{
output = output_;
indent_level = 0;
spaces[0] = 0;
strcpy(spaces+1, " ");
}
void Messages::enter_level(int diff)
{
assert (indent_level >= 0);
spaces[indent_level] = ' ';
indent_level += diff*2;
spaces[indent_level] = 0;
assert (indent_level >= 0);
}
void Messages::vprintf(int kind, const char* format, va_list va)
{
if (!v_message_wanted(kind)) return;
fprintf(output, "%s [%s]%s ", timestamp(), v_format_kind(kind), spaces);
vfprintf(output, format, va);
}
// break a multi-line string into lines (so that we show prefix on each line)
void Messages::vprintf_multiline(int kind, const char* str, const char* prefix_format, va_list va)
{
if (!v_message_wanted(kind)) return;
if (str == NULL) return;
char sprefix[256] = "";
if (prefix_format) {
vsprintf(sprefix, prefix_format, va);
}
const char* now_timestamp = timestamp();
const char* skind = v_format_kind(kind);
string line;
while (*str) {
if (*str == '\n') {
fprintf(output, "%s [%s]%s %s%s\n", now_timestamp, skind, spaces, sprefix, line.c_str());
line.erase();
} else {
line += *str;
}
++str;
}
if (!line.empty()) {
fprintf(output, "%s %s[%s] %s%s\n", now_timestamp, spaces, skind, sprefix, line.c_str());
}
}
void Messages::vprintf_file(int kind, const char* filename, const char* prefix_format, va_list va)
{
if (!v_message_wanted(kind)) return;
char sprefix[256] = "";
if (prefix_format) {
vsprintf(sprefix, prefix_format, va);
}
const char* now_timestamp = timestamp();
const char* skind = v_format_kind(kind);
ifstream f(filename);
if (!f) return;
string line;
while (getline(f, line)) {
fprintf(output, "%s [%s]%s %s%s\n", now_timestamp, skind, spaces, sprefix, line.c_str());
}
}
void Messages::printf(int kind, const char* format, ...)
{
va_list va;
va_start(va, format);
vprintf(kind, format, va);
va_end(va);
}
void Messages::printf_multiline(int kind, const char* str, const char* prefix_format, ...)
{
va_list va;
va_start(va, prefix_format);
vprintf_multiline(kind, str, prefix_format, va);
va_end(va);
}
void Messages::printf_file(int kind, const char* filename, const char* prefix_format, ...)
{
va_list va;
va_start(va, prefix_format);
vprintf_file(kind, filename, prefix_format, va);
va_end(va);
}
void ScopeMessages::printf(const char* format, ...)
{
va_list va;
va_start(va, format);
messages.vprintf(kind, format, va);
va_end(va);
}
void ScopeMessages::printf_multiline(const char* str, const char* prefix_format, ...)
{
va_list va;
va_start(va, prefix_format);
messages.vprintf_multiline(kind, str, prefix_format, va);
va_end(va);
}
void ScopeMessages::printf_file(const char* filename, const char* prefix_format, ...)
{
va_list va;
va_start(va, prefix_format);
messages.vprintf_file(kind, filename, prefix_format, va);
va_end(va);
}