Move all JS interface things into .js.c files with _register() functions.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3671 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
64
src/trace.c
64
src/trace.c
@ -8,14 +8,29 @@
|
||||
|
||||
#include <sqlite3.h>
|
||||
|
||||
#if !defined(_countof)
|
||||
#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
|
||||
#endif
|
||||
|
||||
enum {
|
||||
k_buffer_size = 4 * 1024 * 1024,
|
||||
};
|
||||
|
||||
typedef struct _tf_trace_stack_t tf_trace_stack_t;
|
||||
|
||||
typedef struct _tf_trace_stack_t
|
||||
{
|
||||
const char* names[256];
|
||||
int count;
|
||||
tf_trace_stack_t* next;
|
||||
} tf_trace_stack_t;
|
||||
|
||||
typedef struct _tf_trace_t
|
||||
{
|
||||
char buffer[k_buffer_size];
|
||||
int write_offset;
|
||||
|
||||
tf_trace_stack_t* stack;
|
||||
} tf_trace_t;
|
||||
|
||||
tf_trace_t* tf_trace_create()
|
||||
@ -27,6 +42,12 @@ tf_trace_t* tf_trace_create()
|
||||
|
||||
void tf_trace_destroy(tf_trace_t* trace)
|
||||
{
|
||||
while (trace->stack)
|
||||
{
|
||||
tf_trace_stack_t* stack = trace->stack;
|
||||
trace->stack = stack->next;
|
||||
free(stack);
|
||||
}
|
||||
free(trace);
|
||||
}
|
||||
|
||||
@ -82,8 +103,22 @@ void tf_trace_begin(tf_trace_t* trace, const char* name)
|
||||
return;
|
||||
}
|
||||
|
||||
if (!trace->stack || trace->stack->count + 1 < _countof(trace->stack->names))
|
||||
{
|
||||
tf_trace_stack_t* stack = malloc(sizeof(tf_trace_stack_t));
|
||||
memset(stack, 0, sizeof(*stack));
|
||||
stack->next = trace->stack;
|
||||
trace->stack = stack;
|
||||
}
|
||||
tf_trace_stack_t* stack = trace->stack;
|
||||
while (stack->count == 0 && stack->next && stack->count + 1 < _countof(trace->stack->names))
|
||||
{
|
||||
stack = stack->next;
|
||||
}
|
||||
stack->names[stack->count++] = name;
|
||||
|
||||
char line[1024];
|
||||
int p = snprintf(line, sizeof(line), "{\"ph\": \"B\", \"pid\": %d, \"ts\": %" PRId64 ", \"name\": \"", getpid(), _trace_ts());
|
||||
int p = snprintf(line, sizeof(line), "{\"ph\": \"B\", \"pid\": %d, \"tid\": 0, \"ts\": %" PRId64 ", \"name\": \"", getpid(), _trace_ts());
|
||||
for (const char* c = name; *c && p < (int)sizeof(line); c++)
|
||||
{
|
||||
switch (*c)
|
||||
@ -112,8 +147,33 @@ void tf_trace_end(tf_trace_t* trace)
|
||||
return;
|
||||
}
|
||||
|
||||
tf_trace_stack_t* stack = trace->stack;
|
||||
while (stack && stack->count == 0)
|
||||
{
|
||||
stack = stack->next;
|
||||
}
|
||||
const char* name = stack && stack->count > 0 ? stack->names[stack->count - 1] : NULL;
|
||||
|
||||
char line[1024];
|
||||
int p = snprintf(line, sizeof(line), "{\"ph\": \"E\", \"pid\": %d, \"ts\": %" PRId64 "},", getpid(), _trace_ts());
|
||||
int p = snprintf(line, sizeof(line), "{\"ph\": \"E\", \"pid\": %d, \"tid\": 0, \"ts\": %" PRId64 ", \"name\": \"", getpid(), _trace_ts());
|
||||
for (const char* c = name; *c && p < (int)sizeof(line); c++)
|
||||
{
|
||||
switch (*c)
|
||||
{
|
||||
case '"':
|
||||
case '\\':
|
||||
line[p++] = '\\';
|
||||
if (p < (int)sizeof(line))
|
||||
{
|
||||
line[p++] = *c;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
line[p++] = *c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
p += snprintf(line + p, sizeof(line) - p, "\"},");
|
||||
_trace_append(trace, line, p);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user