Skip to content

Commit a16434b

Browse files
committed
Add simple version logs format that can be enabled from build options (OPTION_BUILD_LOGS_PRETTY).
1 parent 848ce46 commit a16434b

File tree

7 files changed

+127
-33
lines changed

7 files changed

+127
-33
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ option(OPTION_BUILD_SCRIPTS "Build scripts." ON)
8282
option(OPTION_BUILD_SERIALS "Build serials." ON)
8383
option(OPTION_BUILD_DETOURS "Build detours." ON)
8484
option(OPTION_BUILD_PORTS "Build ports." OFF)
85+
option(OPTION_BUILD_LOG_PRETTY "Build logs in a human readable format." ON)
8586
option(OPTION_BUILD_PIC "Build with position independent code." ON)
8687
option(OPTION_BUILD_SECURITY "Build with stack-smashing protection and source fortify." ON)
8788
option(OPTION_BUILD_GUIX "Disable all build system unreproductible operations." OFF)

cmake/CompileOptions.cmake

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,14 @@ endif()
6262
# Compile definitions
6363
#
6464

65+
if(OPTION_BUILD_LOG_PRETTY)
66+
set(LOG_POLICY_FORMAT_PRETTY_VALUE 1)
67+
else()
68+
set(LOG_POLICY_FORMAT_PRETTY_VALUE 0)
69+
endif()
70+
6571
set(DEFAULT_COMPILE_DEFINITIONS
72+
LOG_POLICY_FORMAT_PRETTY=${LOG_POLICY_FORMAT_PRETTY_VALUE}
6673
SYSTEM_${SYSTEM_NAME_UPPER}
6774
)
6875

@@ -72,7 +79,7 @@ if(WIN32)
7279
_SCL_SECURE_NO_WARNINGS # Calling any one of the potentially unsafe methods in the Standard C++ Library
7380
_CRT_SECURE_NO_WARNINGS # Calling any one of the potentially unsafe methods in the CRT Library
7481
)
75-
endif ()
82+
endif()
7683

7784
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR MAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
7885
set(DEFAULT_COMPILE_DEFINITIONS

source/loaders/py_loader/source/py_loader_impl.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,13 +704,15 @@ function_return function_py_interface_invoke(function func, function_impl impl,
704704
if (t == NULL)
705705
{
706706
id = value_type_id((value)args[args_count]);
707+
708+
log_write("metacall", LOG_LEVEL_DEBUG, "Argument #%u Type: %s", args_count, type_id_name(id));
707709
}
708710
else
709711
{
710712
id = type_index(t);
711-
}
712713

713-
log_write("metacall", LOG_LEVEL_DEBUG, "Type (%p): %d", (void *)t, id);
714+
log_write("metacall", LOG_LEVEL_DEBUG, "Argument #%u Type (%p): %s", args_count, (void *)t, type_name(t));
715+
}
714716

715717
py_func->values[args_count] = py_loader_impl_value_to_capi(impl, py_impl, id, args[args_count]);
716718

@@ -741,13 +743,16 @@ function_return function_py_interface_invoke(function func, function_impl impl,
741743
if (ret_type == NULL)
742744
{
743745
id = py_loader_impl_capi_to_value_type(result);
746+
747+
log_write("metacall", LOG_LEVEL_DEBUG, "Return Type: %s", type_id_name(id));
744748
}
745749
else
746750
{
747751
id = type_index(ret_type);
752+
753+
log_write("metacall", LOG_LEVEL_DEBUG, "Return Type (%p): %s", (void *)ret_type, type_name(ret_type));
748754
}
749755

750-
log_write("metacall", LOG_LEVEL_DEBUG, "Return type %p, %d", (void *)ret_type, id);
751756

752757
v = py_loader_impl_capi_to_value(py_func->impl, result, id);
753758

source/log/source/log_policy_format_text.c

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#define LOG_POLICY_FORMAT_TEXT_STR_DEBUG "[%.19s] #%" PRIuS " [ %" PRIuS " | %s | %s ] @%s : "
2424
#define LOG_POLICY_FORMAT_TEXT_STR_RELEASE "[%.19s] #%" PRIuS " @%s : "
25+
#define LOG_POLICY_FORMAT_TEXT_STR_PRETTY "\x1b[32m%s\x1b[0m: "
2526

2627
/* -- Macros -- */
2728

@@ -130,36 +131,57 @@ static size_t log_policy_format_text_size(log_policy policy, const log_record re
130131

131132
static const char * log_policy_format_text_serialize_impl_format(enum log_level_id log_level, unsigned int flags)
132133
{
133-
if (log_level == LOG_LEVEL_DEBUG)
134+
#if (LOG_POLICY_FORMAT_PRETTY == 1)
134135
{
136+
(void)log_level;
137+
135138
if (flags & LOG_POLICY_FORMAT_TEXT_NEWLINE)
136139
{
137-
static const char format_debug_newline[] = LOG_POLICY_FORMAT_TEXT_STR_DEBUG "%s\n";
140+
static const char format_debug_newline[] = LOG_POLICY_FORMAT_TEXT_STR_PRETTY "%s\n";
138141

139142
return format_debug_newline;
140143
}
141144
else
142145
{
143-
static const char format_debug[] = LOG_POLICY_FORMAT_TEXT_STR_DEBUG "%s";
146+
static const char format_debug[] = LOG_POLICY_FORMAT_TEXT_STR_PRETTY "%s";
144147

145148
return format_debug;
146149
}
147150
}
148-
else
151+
#else
149152
{
150-
if (flags & LOG_POLICY_FORMAT_TEXT_NEWLINE)
153+
if (log_level == LOG_LEVEL_DEBUG)
151154
{
152-
static const char format_release_newline[] = LOG_POLICY_FORMAT_TEXT_STR_RELEASE "%s\n";
153-
154-
return format_release_newline;
155+
if (flags & LOG_POLICY_FORMAT_TEXT_NEWLINE)
156+
{
157+
static const char format_debug_newline[] = LOG_POLICY_FORMAT_TEXT_STR_DEBUG "%s\n";
158+
159+
return format_debug_newline;
160+
}
161+
else
162+
{
163+
static const char format_debug[] = LOG_POLICY_FORMAT_TEXT_STR_DEBUG "%s";
164+
165+
return format_debug;
166+
}
155167
}
156168
else
157169
{
158-
static const char format_release[] = LOG_POLICY_FORMAT_TEXT_STR_RELEASE "%s";
159-
160-
return format_release;
170+
if (flags & LOG_POLICY_FORMAT_TEXT_NEWLINE)
171+
{
172+
static const char format_release_newline[] = LOG_POLICY_FORMAT_TEXT_STR_RELEASE "%s\n";
173+
174+
return format_release_newline;
175+
}
176+
else
177+
{
178+
static const char format_release[] = LOG_POLICY_FORMAT_TEXT_STR_RELEASE "%s";
179+
180+
return format_release;
181+
}
161182
}
162183
}
184+
#endif
163185
}
164186

165187
static size_t log_policy_format_text_serialize_impl(log_policy policy, const log_record record, void * buffer, const size_t size)
@@ -206,37 +228,48 @@ static size_t log_policy_format_text_serialize_impl_va(log_policy policy, const
206228
{
207229
log_policy_format_text_data text_data = log_policy_instance(policy);
208230

209-
log_aspect aspect = log_policy_aspect(policy);
210-
211-
log_impl impl = log_aspect_parent(aspect);
212-
213231
int header_length = 0, body_length = 0;
214232

215233
void * buffer_body = NULL;
216234

217235
struct log_record_va_list_type * variable_args;
218236

219-
if (log_impl_level(impl) == LOG_LEVEL_DEBUG)
237+
#if (LOG_POLICY_FORMAT_PRETTY == 1)
220238
{
221-
static const char header_format[] = LOG_POLICY_FORMAT_TEXT_STR_DEBUG;
239+
static const char header_format[] = LOG_POLICY_FORMAT_TEXT_STR_PRETTY;
222240

223241
header_length = snprintf(buffer, size, header_format,
224-
ctime(log_record_time(record)),
225-
log_record_thread_id(record),
226-
log_record_line(record),
227-
log_record_func(record),
228-
log_record_file(record),
229242
log_level_to_string(log_record_level(record)));
230243
}
231-
else
244+
#else
232245
{
233-
static const char header_format[] = LOG_POLICY_FORMAT_TEXT_STR_RELEASE;
246+
log_aspect aspect = log_policy_aspect(policy);
234247

235-
header_length = snprintf(buffer, size, header_format,
236-
ctime(log_record_time(record)),
237-
log_record_thread_id(record),
238-
log_level_to_string(log_record_level(record)));
248+
log_impl impl = log_aspect_parent(aspect);
249+
250+
if (log_impl_level(impl) == LOG_LEVEL_DEBUG)
251+
{
252+
static const char header_format[] = LOG_POLICY_FORMAT_TEXT_STR_DEBUG;
253+
254+
header_length = snprintf(buffer, size, header_format,
255+
ctime(log_record_time(record)),
256+
log_record_thread_id(record),
257+
log_record_line(record),
258+
log_record_func(record),
259+
log_record_file(record),
260+
log_level_to_string(log_record_level(record)));
261+
}
262+
else
263+
{
264+
static const char header_format[] = LOG_POLICY_FORMAT_TEXT_STR_RELEASE;
265+
266+
header_length = snprintf(buffer, size, header_format,
267+
ctime(log_record_time(record)),
268+
log_record_thread_id(record),
269+
log_level_to_string(log_record_level(record)));
270+
}
239271
}
272+
#endif
240273

241274
if (header_length <= 0)
242275
{

source/reflect/include/reflect/reflect_type_id.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ enum type_primitive_id
5151

5252
typedef int type_id;
5353

54+
/**
55+
* @brief
56+
* Obtain a human readable form of the type id @id
57+
*
58+
* @param[in] id
59+
* Type id to be converted into string
60+
*
61+
* @return
62+
* String representing a readable name for @id
63+
*/
64+
REFLECT_API const char * type_id_name(type_id id);
65+
5466
/**
5567
* @brief
5668
* Check if type id is boolean value (bool)

source/reflect/source/reflect_type_id.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,41 @@
2020

2121
#include <reflect/reflect_type_id.h>
2222

23+
#include <portability/portability_assert.h>
24+
25+
26+
static const char * type_id_name_map[] =
27+
{
28+
"Boolean",
29+
"Char",
30+
"Short",
31+
"Int",
32+
"Long",
33+
"Float",
34+
"Double",
35+
"String",
36+
"Buffer",
37+
"Array",
38+
"Map",
39+
"Pointer",
40+
"Future",
41+
"Function",
42+
"Null"
43+
};
44+
45+
static_assert((int) sizeof(type_id_name_map) / sizeof(type_id_name_map[0]) == (int) TYPE_SIZE,
46+
"Size of type id name map does not match the type size");
47+
48+
const char * type_id_name(type_id id)
49+
{
50+
if (id >= 0 && id < TYPE_SIZE)
51+
{
52+
return type_id_name_map[id];
53+
}
54+
55+
return "Invalid";
56+
}
57+
2358
int type_id_boolean(type_id id)
2459
{
2560
return !(id == TYPE_BOOL);

tools/metacall-configure.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ sub_options() {
117117
}
118118

119119
sub_configure() {
120-
BUILD_STRING="-DOPTION_BUILD_LOADERS=On \
120+
BUILD_STRING="-DOPTION_BUILD_LOG_PRETTY=Off \
121+
-DOPTION_BUILD_LOADERS=On \
121122
-DOPTION_BUILD_LOADERS_MOCK=On"
122123

123124
# Scripts

0 commit comments

Comments
 (0)