Add timestamps to messages on stderr (#602)

Closes https://github.com/awesomeWM/awesome/pull/606.
Fixes https://github.com/awesomeWM/awesome/issues/602.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2015-12-22 19:28:05 +01:00 committed by Daniel Hahler
parent fc8c91c075
commit 720cd879f3
7 changed files with 43 additions and 10 deletions

View File

@ -28,6 +28,22 @@
#include <limits.h> #include <limits.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <time.h>
const char *
a_current_time_str(void)
{
static char buffer[25];
time_t now;
struct tm tm;
time(&now);
localtime_r(&now, &tm);
if (!strftime(buffer, sizeof(buffer), "%Y-%m-%d %T ", &tm))
buffer[0] = '\0';
return buffer;
}
/** Print error and exit with EXIT_FAILURE code. /** Print error and exit with EXIT_FAILURE code.
*/ */
@ -37,7 +53,7 @@ _fatal(int line, const char *fct, const char *fmt, ...)
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
fprintf(stderr, "E: awesome: %s:%d: ", fct, line); fprintf(stderr, "%sE: awesome: %s:%d: ", a_current_time_str(), fct, line);
vfprintf(stderr, fmt, ap); vfprintf(stderr, fmt, ap);
va_end(ap); va_end(ap);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
@ -51,7 +67,7 @@ _warn(int line, const char *fct, const char *fmt, ...)
{ {
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
fprintf(stderr, "W: awesome: %s:%d: ", fct, line); fprintf(stderr, "%sW: awesome: %s:%d: ", a_current_time_str(), fct, line);
vfprintf(stderr, fmt, ap); vfprintf(stderr, fmt, ap);
va_end(ap); va_end(ap);
fprintf(stderr, "\n"); fprintf(stderr, "\n");

View File

@ -319,6 +319,8 @@ void _fatal(int, const char *, const char *, ...)
void _warn(int, const char *, const char *, ...) void _warn(int, const char *, const char *, ...)
__attribute__ ((format(printf, 3, 4))); __attribute__ ((format(printf, 3, 4)));
const char *a_current_time_str(void);
void a_exec(const char *); void a_exec(const char *);
#endif #endif

View File

@ -28,6 +28,7 @@ local capi =
awesome = awesome, awesome = awesome,
mouse = mouse mouse = mouse
} }
local gears_debug = require("gears.debug")
local floor = math.floor local floor = math.floor
local util = {} local util = {}
@ -48,11 +49,11 @@ function util.deprecate(see)
-- Get function name/desc from caller. -- Get function name/desc from caller.
local info = debug.getinfo(2, "n") local info = debug.getinfo(2, "n")
local funcname = info.name or "?" local funcname = info.name or "?"
io.stderr:write("W: awful: function " .. funcname .. " is deprecated") local msg = "awful: function " .. funcname .. " is deprecated"
if see then if see then
io.stderr:write(", see " .. see) msg = msg .. ", see " .. see
end end
io.stderr:write(".\n" .. tb .. "\n") gears_debug.print_warning(msg .. ".\n" .. tb)
end end
--- Get a valid color for Pango markup --- Get a valid color for Pango markup

View File

@ -26,6 +26,7 @@ local capi =
screen = screen, screen = screen,
awesome = awesome awesome = awesome
} }
local gears_debug = require("gears.debug")
local xresources = require("beautiful.xresources") local xresources = require("beautiful.xresources")
@ -134,7 +135,7 @@ function beautiful.init(config)
end end
if not success then if not success then
return print("E: beautiful: error loading theme file " .. theme) return gears_debug.print_error("beautiful: error loading theme file " .. theme)
elseif theme then elseif theme then
-- expand '~' -- expand '~'
if homedir then if homedir then
@ -145,10 +146,10 @@ function beautiful.init(config)
if theme.font then set_font(theme.font) end if theme.font then set_font(theme.font) end
else else
return print("E: beautiful: error loading theme file " .. config) return gears_debug.print_error("beautiful: error loading theme file " .. config)
end end
else else
return print("E: beautiful: error loading theme: no theme specified") return gears_debug.print_error("beautiful: error loading theme: no theme specified")
end end
end end

View File

@ -11,6 +11,7 @@
local print = print local print = print
local awesome = awesome local awesome = awesome
local round = require("awful.util").round local round = require("awful.util").round
local gears_debug = require("gears.debug")
local xresources = {} local xresources = {}
@ -53,7 +54,7 @@ function xresources.get_current_theme()
for _, key in ipairs(keys) do for _, key in ipairs(keys) do
colors[key] = awesome.xrdb_get_value("", key) colors[key] = awesome.xrdb_get_value("", key)
if not colors[key] then if not colors[key] then
print("W: beautiful: can't get colorscheme from xrdb (using fallback).") gears_debug.print_warning("beautiful: can't get colorscheme from xrdb (using fallback).")
return fallback return fallback
end end
end end

View File

@ -64,6 +64,18 @@ function debug.dump(data, tag, depth)
print(debug.dump_return(data, tag, depth)) print(debug.dump_return(data, tag, depth))
end end
-- Print an warning message
-- @tparam string message The warning message to print
function debug.print_warning(message)
io.stderr:write(os.date("%Y-%m-%d %T W: ") .. tostring(message) .. "\n")
end
-- Print an error message
-- @tparam string message The error message to print
function debug.print_error(message)
io.stderr:write(os.date("%Y-%m-%d %T E: ") .. tostring(message) .. "\n")
end
return debug return debug
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

2
luaa.h
View File

@ -50,7 +50,7 @@ luaA_warn(lua_State *L, const char *fmt, ...)
{ {
va_list ap; va_list ap;
luaL_where(L, 1); luaL_where(L, 1);
fprintf(stderr, "%sW: ", lua_tostring(L, -1)); fprintf(stderr, "%s%sW: ", a_current_time_str(), lua_tostring(L, -1));
lua_pop(L, 1); lua_pop(L, 1);
va_start(ap, fmt); va_start(ap, fmt);
vfprintf(stderr, fmt, ap); vfprintf(stderr, fmt, ap);