Browse Source

add logging

master
Alexandros Theodotou 3 years ago
parent
commit
1bdbf07c39
Signed by: alex
GPG Key ID: 022EAE42313D70F3
  1. 75
      inc/ztoolkit/log.h
  2. 75
      inc/ztoolkit/ztk_drawing_area.h
  3. 49
      inc/ztoolkit/ztk_widget.h
  4. 113
      src/log.c
  5. 1
      src/meson.build
  6. 21
      src/ztk_widget.c
  7. 51
      tests/helper.h
  8. 31
      tests/log.c
  9. 7
      tests/meson.build

75
inc/ztoolkit/log.h

@ -0,0 +1,75 @@ @@ -0,0 +1,75 @@
/*
* Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of ZToolkit
*
* ZToolkit is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ZToolkit 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU General Affero Public License
* along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file
*
* Logging utilities.
*/
#ifndef __Z_TOOLKIT_LOG_H__
#define __Z_TOOLKIT_LOG_H__
typedef enum ZtkLogLevel
{
ZTK_LOG_LEVEL_DEBUG,
ZTK_LOG_LEVEL_MESSAGE,
ZTK_LOG_LEVEL_WARNING,
ZTK_LOG_LEVEL_ERROR,
} ZtkLogLevel;
static int ztk_log_enabled = 1;
/**
* Enables or disables logging.
*/
static inline void
ztk_log_set_enabled (
int enabled)
{
ztk_log_enabled = enabled;
}
/**
* Logs a message.
*
* @param func Function name.
* @param level Log level.
* @param format The format of the message to log.
*/
void
ztk_log (
const char * func,
ZtkLogLevel level,
const char * format,
...);
#define ztk_debug(msg) \
ztk_log (__func__, ZTK_LOG_LEVEL_DEBUG, msg)
#define ztk_message(msg) \
ztk_log (__func__, ZTK_LOG_LEVEL_MESSAGE, msg)
#define ztk_warning(msg) \
ztk_log (__func__, ZTK_LOG_LEVEL_WARNING, msg)
#define ztk_error(msg) \
ztk_log (__func__, ZTK_LOG_LEVEL_ERROR, msg)
#endif

75
inc/ztoolkit/ztk_drawing_area.h

@ -0,0 +1,75 @@ @@ -0,0 +1,75 @@
/*
* Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of ZToolkit
*
* ZToolkit is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ZToolkit 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU General Affero Public License
* along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file
*
* Widget for custom drawing.
*/
#ifndef __Z_TOOLKIT_ZTK_DRAWING_AREA_H__
#define __Z_TOOLKIT_ZTK_DRAWING_AREA_H__
#include "ztoolkit/ztk_widget.h"
/**
* @addtogroup ztoolkit
*
* @{
*/
/**
* Drawing area widget.
*
* This is used for custom drawing.
*/
typedef struct ZtkDrawingArea
{
/** Base widget. */
ZtkWidget base;
} ZtkDrawingArea;
/**
* Creates a new ZtkDrawingArea.
*
* @param update_cb Optional callback to be called
* for updating the user data based on the current
* state of the widget. See ztk_knob.c for an
* example.
* @param draw_cb Draw callback. This will be called
* when the widget needs to redraw itself.
* @param dispose_cb Optional callback to be called
* when disposing the widget. This will be called
* right before freeing the widget.
* @param data User data.
*/
ZtkDrawingArea *
ztk_drawing_area_new (
PuglRect * rect,
ZtkWidgetCallback update_cb,
ZtkWidgetCallback draw_cb,
ZtkWidgetCallback dispose_cb,
void * data);
/**
* @}
*/
#endif

49
inc/ztoolkit/ztk_widget.h

@ -1,20 +1,20 @@ @@ -1,20 +1,20 @@
/*
* Copyright (C) 2019 Alexandros Theodotou <alex at zrythm dot org>
* Copyright (C) 2019-2020 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of ZPlugins
* This file is part of ZToolkit
*
* ZPlugins is free software: you can redistribute it and/or modify
* ZToolkit is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ZPlugins is distributed in the hope that it will be useful,
* ZToolkit 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU General Affero Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
*/
/**
@ -62,15 +62,26 @@ typedef enum ZtkWidgetState @@ -62,15 +62,26 @@ typedef enum ZtkWidgetState
ZTK_WIDGET_STATE_PRESSED = 1 << 3,
} ZtkWidgetState;
//typedef enum ZtkWidgetType
//{
//ZTK_WIDGET_TYPE_KNOB,
//ZTK_WIDGET_TYPE_CUSTOM,
//} ZtkWidgetType;
//
typedef enum ZtkWidgetType
{
ZTK_WIDGET_TYPE_NONE,
ZTK_WIDGET_TYPE_LABEL,
ZTK_WIDGET_TYPE_KNOB,
ZTK_WIDGET_TYPE_DRAWING_AREA,
} ZtkWidgetType;
typedef struct ZtkWidget ZtkWidget;
/**
* Prototype for callbacks.
*
* @param widget The ZtkWidget instance.
* @param data User data passed when instantiating the drawing area.
*/
typedef void (*ZtkWidgetCallback) (
ZtkWidget * widget,
void * data);
/**
* Base widget.
*/
@ -86,11 +97,8 @@ typedef struct ZtkWidget @@ -86,11 +97,8 @@ typedef struct ZtkWidget
/**
* The type this widget is.
*
* Custom widgets should be
* ZTK_WIDGET_TYPE_CUSTOM + increasing numbers.
*/
//ZtkWidgetType type;
ZtkWidgetType type;
/** Update callback (called right before drawing)
* (required. */
@ -147,6 +155,9 @@ typedef struct ZtkWidget @@ -147,6 +155,9 @@ typedef struct ZtkWidget
cairo_t * cached_cr;
cairo_surface_t * cached_surface;
/** User data. */
void * user_data;
} ZtkWidget;
/**
@ -172,6 +183,14 @@ ztk_widget_is_hit ( @@ -172,6 +183,14 @@ ztk_widget_is_hit (
double x,
double y);
/**
* Sets the user data.
*/
void
ztk_widget_set_user_data (
ZtkWidget * self,
void * data);
/**
* Draws the widget.
*/

113
src/log.c

@ -0,0 +1,113 @@ @@ -0,0 +1,113 @@
/*
* Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of ZToolkit
*
* ZToolkit is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ZToolkit 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU General Affero Public License
* along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <ztoolkit/log.h>
/* ANSI color codes */
#define COLOR_RED "\x1b[31m"
#define COLOR_GREEN "\x1b[32m"
#define COLOR_YELLOW "\x1b[33m"
#define COLOR_BLUE "\x1b[34m"
#define COLOR_MAGENTA "\x1b[35m"
#define COLOR_CYAN "\x1b[36m"
#define COLOR_RESET "\x1b[0m"
/**
* Fills in the timestamp as a string into \ref buf.
*/
static void
get_timestamp (
char * buf)
{
struct timeval curTime;
gettimeofday(&curTime, NULL);
int milli = curTime.tv_usec / 1000;
char buffer [80];
strftime (
buffer, 80, "%H:%M:%S",
localtime (&curTime.tv_sec));
sprintf(buf, "%s:%03d", buffer, milli);
}
/**
* Logs a message.
*
* @param func Function name.
* @param level Log level.
* @param format The format of the message to log.
*/
void
ztk_log (
const char * func,
ZtkLogLevel level,
const char * format,
...)
{
va_list arg;
char buf[6000] = "** ";
switch (level)
{
case ZTK_LOG_LEVEL_DEBUG:
strcat (
buf, COLOR_CYAN "DEBUG: " COLOR_RESET);
break;
case ZTK_LOG_LEVEL_MESSAGE:
strcat (
buf,
COLOR_GREEN "MESSAGE: " COLOR_RESET);
break;
case ZTK_LOG_LEVEL_WARNING:
strcat (
buf,
COLOR_YELLOW "WARNING: " COLOR_RESET);
break;
case ZTK_LOG_LEVEL_ERROR:
strcat (
buf, COLOR_RED "ERROR: " COLOR_RESET);
break;
default:
break;
}
/* get current time as string*/
char cur_time[200];
get_timestamp (cur_time);
strcat (buf, cur_time);
strcat (buf, ": ");
/* format message */
char log_msg[6000];
va_start (arg, format);
vsprintf (log_msg, format, arg);
va_end (arg);
strcat (buf, log_msg);
fprintf (stderr, "%s\n", buf);
}

1
src/meson.build

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
# along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
ztoolkit_srcs = files([
'log.c',
'ztk_app.c',
'ztk_color.c',
'ztk_knob.c',

21
src/ztk_widget.c

@ -1,20 +1,20 @@ @@ -1,20 +1,20 @@
/*
* Copyright (C) 2019 Alexandros Theodotou <alex at zrythm dot org>
* Copyright (C) 2019-2020 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of ZPlugins
* This file is part of ZToolkit
*
* ZPlugins is free software: you can redistribute it and/or modify
* ZToolkit is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* ZPlugins is distributed in the hope that it will be useful,
* ZToolkit 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU General Affero Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
*/
#include "ztoolkit/ztk_widget.h"
@ -55,6 +55,17 @@ ztk_widget_is_hit ( @@ -55,6 +55,17 @@ ztk_widget_is_hit (
y <= self->rect.y + self->rect.height;
}
/**
* Sets the user data.
*/
void
ztk_widget_set_user_data (
ZtkWidget * self,
void * data)
{
self->user_data = data;
}
/**
* Draws the widget.
*/

51
tests/helper.h

@ -0,0 +1,51 @@ @@ -0,0 +1,51 @@
/*
* Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org>
* This file is part of ZToolkit
* ZToolkit is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* ZToolkit 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 Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef __Z_TOOLKIT_TESTS_HELPER_H__
#define __Z_TOOLKIT_TESTS_HELPER_H__
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ztoolkit/log.h>
#define ztk_assert(x) \
if (!(x)) \
{ \
ztk_error ("Assertion failed: %s", #x); \
exit (1); \
}
int
str_endswith (
const char *s, const char *t)
{
size_t ls = strlen(s); // find length of s
size_t lt = strlen(t); // find length of t
if (ls >= lt) // check if t can fit in s
{
// point s to where t should start and compare the strings from there
return (0 == memcmp(t, s + (ls - lt), lt));
}
return 0; // t was longer than s
}
#endif

31
tests/log.c

@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
/*
* Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of ZToolkit
*
* ZToolkit is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ZToolkit 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
*/
#include "helper.h"
int main (
int argc, const char* argv[])
{
ztk_debug ("debug test");
ztk_message ("message test");
ztk_warning ("warning test");
ztk_error ("error test");
return 0;
}

7
tests/meson.build

@ -15,4 +15,9 @@ @@ -15,4 +15,9 @@
# You should have received a copy of the GNU Affero General Public License
# along with ZToolkit. If not, see <https://www.gnu.org/licenses/>.
# TODO
e = executable (
'log_exe', 'log.c',
include_directories: inc_dirs,
link_with: ztoolkit_lib,
)
test ('log_test', e)

Loading…
Cancel
Save