Skip to content
Snippets Groups Projects
Commit 864fc3a2 authored by Jiří Kalvoda's avatar Jiří Kalvoda
Browse files

Added logging message to files.

parent f3b7c40f
No related branches found
No related tags found
No related merge requests found
...@@ -53,6 +53,11 @@ You can also add attributes to the message (written as command-line options): ...@@ -53,6 +53,11 @@ You can also add attributes to the message (written as command-line options):
--to=stdout Write message to stdout instead of display. --to=stdout Write message to stdout instead of display.
--log=mask Write this message to log file. Bitmask.
Last bit: Write to ~/.osdd_last in machine readable form.
Second bit: Append to ~/.osdd_log in human readable form with RGB color.
The default values of most of these attributes can be given by command-line The default values of most of these attributes can be given by command-line
options of the daemon, use `osdd --help' to get the full list. options of the daemon, use `osdd --help' to get the full list.
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include <time.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/Xatom.h> #include <X11/Xatom.h>
#include <X11/extensions/shape.h> #include <X11/extensions/shape.h>
...@@ -377,10 +378,89 @@ static void osd_draw_line(struct osd_state *osd, int i) ...@@ -377,10 +378,89 @@ static void osd_draw_line(struct osd_state *osd, int i)
XftColorFree(osd->dpy, osd->visual, osd->cmap, &osd->mask_color); XftColorFree(osd->dpy, osd->visual, osd->cmap, &osd->mask_color);
} }
static void repace_bad_char(char * in)
{
for(;*in;in++)
{
if('a'<= *in&& *in<='z') continue;
if('A'<= *in&& *in<='Z') continue;
if('0'<= *in&& *in<='9') continue;
if(128 <= (unsigned char)*in) continue;
*in=' ';
}
}
static void osd_log_to_file(struct osd_state *osd)
{
#define MAXIMUM_FILE_MANE_LEN 1024
int need_log=0;
for(int i=0;i<osd->num_lines;i++) need_log|=osd->lines[i].log;
if(need_log&1)
{
char name[MAXIMUM_FILE_MANE_LEN];
snprintf(name,MAXIMUM_FILE_MANE_LEN,"%s/.osdd_last",getenv("HOME"));
FILE * f = fopen(name,"w");
if(f)
{
fprintf(f,"%lld\n",(long long)time(0));
for(int i=0;i<osd->num_lines;i++)
{
struct osd_line * line = osd->lines+i;
if(!(line->log & 1)) continue;
if(line->type!=OSD_TYPE_TEXT) continue;
// Allocate colors
XftColor outline_color;
XRenderColor mask_rc = { .red = 0xffff, .green = 0xffff, .blue = 0xffff, .alpha = 0xffff };
if (!XftColorAllocName(osd->dpy, osd->visual, osd->cmap, line->fg_color, &osd->fg_color) ||
!XftColorAllocName(osd->dpy, osd->visual, osd->cmap, line->outline_color, &outline_color) ||
!XftColorAllocValue(osd->dpy, osd->visual, osd->cmap, &mask_rc, &osd->mask_color))
die("Cannot allocate colors");
typeof(osd->fg_color.color) c = osd->fg_color.color;
//fprintf(stderr,"%02x%02x%02x %s\n",c.red/256,c.green/256,c.blue/256,osd->lines[i].u.text);
char text[1024];
snprintf(text,1000,osd->lines[i].u.text);
repace_bad_char(text);
fprintf(f,"%02x%02x%02x %s\n",c.red/256,c.green/256,c.blue/256,text);
}
fclose(f);
}
}
if(need_log&2)
{
char name[MAXIMUM_FILE_MANE_LEN];
snprintf(name,MAXIMUM_FILE_MANE_LEN,"%s/.osdd_log",getenv("HOME"));
FILE * f = fopen(name,"a");
if(f)
{
time_t t = time(NULL);
struct tm tm = *localtime(&t);
fprintf(f,"%04d-%02d-%02d %02d:%02d:%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
for(int i=0;i<osd->num_lines;i++)
{
struct osd_line * line = osd->lines+i;
if(!(line->log & 2)) continue;
// Allocate colors
XftColor outline_color;
XRenderColor mask_rc = { .red = 0xffff, .green = 0xffff, .blue = 0xffff, .alpha = 0xffff };
if (!XftColorAllocName(osd->dpy, osd->visual, osd->cmap, line->fg_color, &osd->fg_color) ||
!XftColorAllocName(osd->dpy, osd->visual, osd->cmap, line->outline_color, &outline_color) ||
!XftColorAllocValue(osd->dpy, osd->visual, osd->cmap, &mask_rc, &osd->mask_color))
die("Cannot allocate colors");
typeof(osd->fg_color.color) c = osd->fg_color.color;
//fprintf(stderr,"%02x%02x%02x %s\n",c.red/256,c.green/256,c.blue/256,osd->lines[i].u.text);
fprintf(f," \x1b[38;2;%d;%d;%dm%s\e[0m",c.red/256,c.green/256,c.blue/256,osd->lines[i].u.text);
}
fprintf(f,"\n");
fclose(f);
}
}
}
void osd_show(struct osd_state *osd) void osd_show(struct osd_state *osd)
{ {
osd_hide(osd); osd_hide(osd);
osd_prepare(osd); osd_prepare(osd);
osd_log_to_file(osd);
// Create our window // Create our window
XSetWindowAttributes win_attr = { XSetWindowAttributes win_attr = {
......
...@@ -35,6 +35,7 @@ struct osd_line { ...@@ -35,6 +35,7 @@ struct osd_line {
int slider_unit; int slider_unit;
int slider_space; int slider_space;
int slider_units; int slider_units;
int log;
}; };
struct osd_state *osd_new(Display *dpy); struct osd_state *osd_new(Display *dpy);
......
#!/bin/sh #!/bin/sh
date '+%d-%m-%Y %H:%M:%S' | osdc - date '+%Y-%m-%d %H:%M:%S' | osdc --log=0 --min-duration=1 -
...@@ -30,6 +30,7 @@ static char *default_outline_color = "black"; ...@@ -30,6 +30,7 @@ static char *default_outline_color = "black";
static int default_outline_width = 2; static int default_outline_width = 2;
static int default_duration = 1000; static int default_duration = 1000;
static int default_min_duration = 250; static int default_min_duration = 250;
static int default_log = 255;
static int debug_mode; static int debug_mode;
static int test_mode; static int test_mode;
static double line_spacing = 0.2; static double line_spacing = 0.2;
...@@ -49,6 +50,7 @@ static const struct option long_opts[] = { ...@@ -49,6 +50,7 @@ static const struct option long_opts[] = {
{ "outline-color", required_argument, NULL, 'o' }, { "outline-color", required_argument, NULL, 'o' },
{ "outline-width", required_argument, NULL, 'O' }, { "outline-width", required_argument, NULL, 'O' },
{ "line-spacing", required_argument, NULL, 's' }, { "line-spacing", required_argument, NULL, 's' },
{ "log", required_argument, NULL, 'L' },
{ "test", no_argument, NULL, OPT_TEST }, // Undocumented test mode { "test", no_argument, NULL, OPT_TEST }, // Undocumented test mode
{ NULL, 0, NULL, 0 }, { NULL, 0, NULL, 0 },
}; };
...@@ -66,6 +68,7 @@ Options:\n\ ...@@ -66,6 +68,7 @@ Options:\n\
-o, --outline-color=<c>\tDefault outline color\n\ -o, --outline-color=<c>\tDefault outline color\n\
-O, --outline-width=<n>\tDefault outline width (default=2)\n\ -O, --outline-width=<n>\tDefault outline width (default=2)\n\
-s, --line-spacing=<n>\tSet line spacing factor (decimal fraction, default=0.2)\n\ -s, --line-spacing=<n>\tSet line spacing factor (decimal fraction, default=0.2)\n\
-L, --log=<n>\t\tSet default log options (bitmask, default=255)\n\
"); ");
exit(1); exit(1);
} }
...@@ -101,6 +104,9 @@ parse_opts(int argc, char **argv) ...@@ -101,6 +104,9 @@ parse_opts(int argc, char **argv)
case 'O': case 'O':
default_outline_width = atoi(optarg); default_outline_width = atoi(optarg);
break; break;
case 'L':
default_log = atoi(optarg);
break;
case OPT_TEST: case OPT_TEST:
test_mode = 1; test_mode = 1;
debug_mode = 1; debug_mode = 1;
...@@ -130,6 +136,7 @@ display_msg(struct msg *msg) ...@@ -130,6 +136,7 @@ display_msg(struct msg *msg)
char *fg_color = default_color; char *fg_color = default_color;
char *outline_color = default_outline_color; char *outline_color = default_outline_color;
int outline_width = default_outline_width; int outline_width = default_outline_width;
int log = default_log;
char *line = msg->text; char *line = msg->text;
while (*line) while (*line)
...@@ -172,6 +179,8 @@ display_msg(struct msg *msg) ...@@ -172,6 +179,8 @@ display_msg(struct msg *msg)
msg->max_light = now + atoi(val); msg->max_light = now + atoi(val);
else if (!strcmp(key, "min-duration")) else if (!strcmp(key, "min-duration"))
msg->min_light = now + atoi(val); msg->min_light = now + atoi(val);
else if (!strcmp(key, "log"))
log = atoi(val);
else if (!strcmp(key, "color")) else if (!strcmp(key, "color"))
fg_color = val; fg_color = val;
else if (!strcmp(key, "outline-color")) else if (!strcmp(key, "outline-color"))
...@@ -183,6 +192,7 @@ display_msg(struct msg *msg) ...@@ -183,6 +192,7 @@ display_msg(struct msg *msg)
l->fg_color = fg_color; l->fg_color = fg_color;
l->outline_color = outline_color; l->outline_color = outline_color;
l->outline_width = outline_width; l->outline_width = outline_width;
l->log = log;
} }
line = nl; line = nl;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment