#ifndef LOG_H
#define LOG_H

/*
 * @(#)Log.h
 *
 * This file is part of webCDwriter - Network CD Writing.
 *
 * Copyright (C) 1999-2004 Jörg P. M. Haeger
 *
 * webCDwriter is free software. See CDWserver.cpp for details.
 *
 * Jörg Haeger, 27.05.1999
 */

#include <pthread.h>
#include <stdio.h>
#include "StringBuffer.h"

/**
 * A Logging System
 *
 * @version 20040503
 * @author Jörg P. M. Haeger
 */
class Log {
	pthread_mutex_t mutex;
	int counter;
	String lastLine;
	int lastLineLevel;
	int lastTime;
	FILE *outStream;
	StringBuffer buf;
	int mark0, mark1, mark2, linesCounter;
	int initMark;
	const class Thread *thread;

public:
	Log();

	// conditional logging of debug messages
	void debug(const char *tag, const char *format, ...);
	void debug(const char *tag, String &message);

	void error(String &message);

	String *getSupportLog();

	/**
	 * log with respect to the current log level
	 */
	void put(int level, const char *message);
	void put(int level, String &message);

	// log important messages
	void put(const char *format, ...);

	// log all messages
	void putv(const char *format, ...);

	/**
	 * Reopen the log file
	 */
	void restart();

	void start(int toStdOut = 0);

private:
	void appendFormatted(StringBuffer &workBuf,
			int n, int width, char ch) {
		int m = n;
		if (m == 0)
			width--;
		for (int i = 0; i < width; i++) {
			if (m == 0)
				workBuf.append(ch);
			m /= 10;
		}
		workBuf.append(n);
	}

	void writeLine(int level, String &line);
};

extern Log log;

#endif
