//
// BwgBurn - CD-R/CD-RW/DVD-R/DVD-RW burning program for Windows XP
//
// Copyright (C) 2006 by Jack W. Griffin (butchg@comcast.net)
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the
//
// Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330,
// Boston, MA 02111-1307 USA
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Bwg.Logging
{
///
/// This class allows for the logging of messages
///
public class Logger
{
///
/// This is used to lock the logger when multiple threads are
/// trying to log messages concurrently
///
private Object m_lock_object;
///
/// This is a map from message type to sinks
///
private IDictionary m_sinks;
///
/// This is a map from the message type to the level that is being filtered. Anything
/// below the value found here is not displayed
///
private IDictionary m_level;
///
/// Create the logging object
///
public Logger()
{
m_lock_object = new Object();
m_sinks = new Dictionary();
m_level = new Dictionary();
}
///
/// Assocated a sink with a given type of message
///
/// the category of message
/// the sink for this category of message
public void SetSink(UserMessage.Category c, Sink s)
{
lock (m_lock_object)
{
m_sinks[c] = s;
}
}
///
/// Return the sink given the type of message
///
/// the type of sink
/// the sink associated with the message type
public Sink GetSink(UserMessage.Category c)
{
Sink s = null;
lock (m_lock_object)
{
m_sinks.TryGetValue(c, out s);
}
return s;
}
///
/// Sets a level, below which messages are filtered
///
/// the category to filter
/// the level to display
public void SetLevel(UserMessage.Category c, int level)
{
lock (m_lock_object)
{
m_level[c] = level;
}
}
///
/// Given the category, this method returns the current
/// level for this type of message
///
/// the type of message
/// the current level
public int GetLevel(UserMessage.Category c)
{
int ret = 0;
lock (m_lock_object)
{
if (m_level.ContainsKey(c))
ret = m_level[c];
}
return ret;
}
///
/// Remove the sink associated with a specific category of message.
///
/// the category of message
public void RemoveSink(UserMessage.Category c)
{
lock (m_lock_object)
{
m_sinks.Remove(c);
}
}
///
/// This function returns TRUE if the logger has a message sink for the given
/// category of message. Otherwise it returns FALSE.
///
/// the category of interest
/// true if a sink is present, false otherwise
public bool HasSink(UserMessage.Category c)
{
return m_sinks.Keys.Contains(c);
}
///
/// Log a message
///
/// the message to log
public void LogMessage(UserMessage m)
{
lock (m_lock_object)
{
if (m_sinks.ContainsKey(m.MType))
{
int level = 0;
if (m_level.ContainsKey(m.MType))
level = m_level[m.MType];
if (m.Level <= level)
{
Sink s = m_sinks[m.MType];
s.LogMessage(m);
}
}
}
}
///
/// Dump a data buffer to the log file, used for debugging
///
/// the log level
/// the title
/// the buffer
/// the buffer size
public void DumpBuffer(uint loglevel, string title, IntPtr buffer, int size)
{
UserMessage m;
m = new UserMessage(UserMessage.Category.Debug, loglevel, "Dumping data for structure '" + title + "'");
LogMessage(m);
int index = 0;
int linebytes = 0;
string str = string.Empty;
while (index < size)
{
if (linebytes == 16)
{
m = new UserMessage(UserMessage.Category.Debug, loglevel, str);
LogMessage(m);
linebytes = 0;
str = string.Empty;
}
byte b = Marshal.ReadByte(buffer, index++);
str += b.ToString("X2") + " ";
linebytes++;
}
if (linebytes != 0)
{
m = new UserMessage(UserMessage.Category.Debug, loglevel, str);
LogMessage(m);
}
}
}
}