grim/guifications3
Clone
Summary
Browse
Changes
Graph
added the install prefix to gflib-genheader
cmake
2010-12-13, Gary Kramlich
999ee3e165df
added the install prefix to gflib-genheader
/*
* Guifications - The end-all, be-all notification framework
* Copyright (C) 2003-2009 Gary Kramlich <grim@reaperworld.com>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include
<gflib/gf_log.h>
#include
<gflib/gf_intl.h>
#include
<gflib/gf_console_logger.h>
#include
<stdlib.h>
/******************************************************************************
* Globals
*****************************************************************************/
static
GfLogger
*
logger
=
NULL
;
/******************************************************************************
* Private API
*****************************************************************************/
void
gf_log_init
(
void
)
{
GfLogger
*
logger
=
NULL
;
GfLogLevel
level
=
GF_LOG_LEVEL_INFO
;
const
gchar
*
envvar
=
NULL
;
if
((
envvar
=
g_getenv
(
"GF_LOG_LEVEL"
)))
{
if
(
!
g_utf8_collate
(
envvar
,
"verbose"
))
level
=
GF_LOG_LEVEL_VERBOSE
;
else
if
(
!
g_utf8_collate
(
envvar
,
"info"
))
level
=
GF_LOG_LEVEL_INFO
;
else
if
(
!
g_utf8_collate
(
envvar
,
"event"
))
level
=
GF_LOG_LEVEL_EVENT
;
else
if
(
!
g_utf8_collate
(
envvar
,
"warning"
))
level
=
GF_LOG_LEVEL_WARNING
;
else
if
(
!
g_utf8_collate
(
envvar
,
"critical"
))
level
=
GF_LOG_LEVEL_CRITICAL
;
}
logger
=
gf_console_logger_new
();
gf_logger_set_level
(
logger
,
level
);
gf_log_set_logger
(
logger
);
g_object_unref
(
G_OBJECT
(
logger
));
}
void
gf_log_uninit
(
void
)
{
gf_log_set_logger
(
NULL
);
}
/******************************************************************************
* API
*****************************************************************************/
/**
* gf_log_add_g_log_handlers:
*
* Makes the glog handlers use the gf_log api instead of outputing right to
* stdout.
*
* This should only be used for applications where the glib mainloop is not the
* default mainloop. In other words, don't call it during startup for plugins
* in other glib/gtk based applications.
*/
void
gf_log_add_g_log_handlers
(
void
)
{
gf_log_set_g_log_handler
(
NULL
);
gf_log_set_g_log_handler
(
"GLib"
);
gf_log_set_g_log_handler
(
"GLib-GObject"
);
gf_log_set_g_log_handler
(
"GModule"
);
gf_log_set_g_log_handler
(
"GThread"
);
g_log_set_default_handler
(
gf_log_g_log_handler
,
NULL
);
}
/**
* gf_log_set_logger:
* @new_logger : The new #GfLogger instance to use.
*
* Sets the default logger for logging.
*/
void
gf_log_set_logger
(
GfLogger
*
new_logger
)
{
if
(
GF_IS_LOGGER
(
logger
))
g_object_unref
(
logger
);
logger
=
GF_IS_LOGGER
(
new_logger
)
?
g_object_ref
(
G_OBJECT
(
new_logger
))
:
NULL
;
}
/**
* gf_log_get_logger:
*
* Gets the default #GfLogger.
*
* Return Value: The default #GfLogger.
*/
GfLogger
*
gf_log_get_logger
(
void
)
{
return
logger
;
}
/**
* gf_log_set_timestamp_format:
* @format : The new format to use for logging.
*
* Sets the timestamp format to use for the current default #GfLogger.
*
* Note: This follows strftime formatting.
*/
void
gf_log_set_timestamp_format
(
const
gchar
*
format
)
{
if
(
logger
)
gf_logger_set_timestamp_format
(
logger
,
format
);
}
/**
* gf_log_get_timestamp_format:
*
* Gets the timestamp format being use on the default #GfLogger.
*
* Return Value: The timestamp format in use on the default #GfLogger.
*/
const
gchar
*
gf_log_get_timestamp_format
(
void
)
{
if
(
logger
)
return
gf_logger_get_timestamp_format
(
logger
);
return
NULL
;
}
/**
* gf_log_toggle_timestamps:
*
* Toggles whether or not timestamps are enabled on the default #GfLogger.
*/
void
gf_log_toggle_timestamps
(
void
)
{
if
(
logger
)
gf_logger_toggle_timestamps
(
logger
);
}
/**
* gf_log_show_timestamps:
*
* Gets whether or not timestamps are being shown on the default #GfLogger.
*
* Return Value: @TRUE if timestamps are enabled, @FALSE otherwise.
*/
gboolean
gf_log_show_timestamps
(
void
)
{
if
(
logger
)
return
gf_logger_show_timestamps
(
logger
);
return
FALSE
;
}
/**
* gf_log_set_level:
* @level : The new #GfLogLevel to set.
*
* Sets the default #GfLogLevel for the default #GfLogger.
*/
void
gf_log_set_level
(
GfLogLevel
level
)
{
if
(
logger
)
gf_logger_set_level
(
logger
,
level
);
}
/**
* gf_log_get_level:
*
* Gets the #GfLogLevel for the current default #GfLogger.
*
* Return Value: The #GfLogLevel for the current default #GfLogger.
*/
GfLogLevel
gf_log_get_level
(
void
)
{
if
(
logger
)
return
gf_logger_get_level
(
logger
);
return
GF_LOG_LEVEL_UNKNOWN
;
}
/**
* gf_log:
* @level : The #GfLogLevel of the message.
* @category : The category of the message.
* @format : The format of the message.
* @args : The arguments for @format.
*
* Writes a message to the default #GfLogger.
*/
void
gf_log
(
GfLogLevel
level
,
const
gchar
*
category
,
const
gchar
*
format
,
va_list
args
)
{
g_return_if_fail
(
format
);
if
(
!
logger
)
return
;
if
(
level
>=
gf_logger_get_level
(
logger
))
gf_logger_write
(
logger
,
level
,
category
,
format
,
args
);
if
(
level
==
GF_LOG_LEVEL_CRITICAL
)
abort
();
}
/**
* gf_logv:
* @level : The #GfLogLevel of the message.
* @category : The category of the message.
* @format : The format of the message.
* @Varargs : The arguments for @format.
*
* Writes a message to the default #GfLogger.
*/
void
gf_logv
(
GfLogLevel
level
,
const
gchar
*
category
,
const
gchar
*
format
,
...)
{
va_list
vargs
;
g_return_if_fail
(
format
);
va_start
(
vargs
,
format
);
gf_log
(
level
,
category
,
format
,
vargs
);
va_end
(
vargs
);
}
/**
* gf_log_critical:
* @category : The category of the message.
* @format : The format of the message.
* @Varargs : The arguments for @format.
*
* Writes a critical message to the default #GfLogger.
*/
void
gf_log_critical
(
const
gchar
*
category
,
const
gchar
*
format
,
...)
{
va_list
vargs
;
g_return_if_fail
(
format
);
va_start
(
vargs
,
format
);
gf_log
(
GF_LOG_LEVEL_CRITICAL
,
category
,
format
,
vargs
);
va_end
(
vargs
);
}
/**
* gf_log_warning:
* @category : The category of the message.
* @format : The format of the message.
* @Varargs : The arguments for @format.
*
* Writes a warning message to the default #GfLogger.
*/
void
gf_log_warning
(
const
gchar
*
category
,
const
gchar
*
format
,
...)
{
va_list
vargs
;
g_return_if_fail
(
format
);
va_start
(
vargs
,
format
);
gf_log
(
GF_LOG_LEVEL_WARNING
,
category
,
format
,
vargs
);
va_end
(
vargs
);
}
/**
* gf_log_event:
* @category : The category of the message.
* @format : The format of the message.
* @Varargs : The arguments for @format.
*
* Writes an event message to the default #GfLogger.
*/
void
gf_log_event
(
const
gchar
*
category
,
const
gchar
*
format
,
...)
{
va_list
vargs
;
g_return_if_fail
(
format
);
va_start
(
vargs
,
format
);
gf_log
(
GF_LOG_LEVEL_EVENT
,
category
,
format
,
vargs
);
va_end
(
vargs
);
}
/**
* gf_log_info:
* @category : The category of the message.
* @format : The format of the message.
* @Varargs : The arguments for @format.
*
* Writes an info message to the default #GfLogger.
*/
void
gf_log_info
(
const
gchar
*
category
,
const
gchar
*
format
,
...)
{
va_list
vargs
;
g_return_if_fail
(
format
);
va_start
(
vargs
,
format
);
gf_log
(
GF_LOG_LEVEL_INFO
,
category
,
format
,
vargs
);
va_end
(
vargs
);
}
/**
* gf_log_verbose:
* @category : The category of the message.
* @format : The format of the message.
* @Varargs : The arguments for @format.
*
* Writes a verbose message to the default #GfLogger.
*/
void
gf_log_verbose
(
const
gchar
*
category
,
const
gchar
*
format
,
...)
{
va_list
vargs
;
g_return_if_fail
(
format
);
va_start
(
vargs
,
format
);
gf_log
(
GF_LOG_LEVEL_VERBOSE
,
category
,
format
,
vargs
);
va_end
(
vargs
);
}
/**
* gf_log_g_log_handler:
* @domain : The log domain of the message.
* @flags : The log level of the message.
* @message : The message.
* @data : User data.
*
* This is a GLogFunc meant to integrate any GLog message into the GfLog API.
*
* If you're using any libraries that use g_log_* use #g_log_set_handler to
* set this function as the handler and it will correctly get propagated to the
* default #GfLogger.
*/
void
gf_log_g_log_handler
(
const
gchar
*
domain
,
GLogLevelFlags
flags
,
const
gchar
*
message
,
gpointer
data
)
{
GfLogLevel
level
=
GF_LOG_LEVEL_UNKNOWN
;
switch
(
flags
)
{
case
G_LOG_LEVEL_ERROR
:
case
G_LOG_LEVEL_CRITICAL
:
/* error is fatal, but critical _should_ be fatal, so these are
* grouped together for now. If this causes problems, we can
* always adjust later.
*/
level
=
GF_LOG_LEVEL_CRITICAL
;
break
;
case
G_LOG_LEVEL_WARNING
:
level
=
GF_LOG_LEVEL_WARNING
;
break
;
case
G_LOG_LEVEL_MESSAGE
:
case
G_LOG_LEVEL_INFO
:
level
=
GF_LOG_LEVEL_INFO
;
break
;
case
G_LOG_LEVEL_DEBUG
:
level
=
GF_LOG_LEVEL_VERBOSE
;
break
;
default
:
break
;
}
gf_logv
(
level
,
domain
,
"%s
\n
"
,
message
);
}