summaryrefslogtreecommitdiffstats
path: root/src/gallium/auxiliary/util/u_log.c
blob: 095421edd064a5de13342a0e9d40414d8a03bd40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
 * Copyright 2017 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "u_log.h"

#include "util/u_memory.h"
#include "util/u_string.h"

struct page_entry {
   const struct u_log_chunk_type *type;
   void *data;
};

struct u_log_page {
   struct page_entry *entries;
   unsigned num_entries;
   unsigned max_entries;
};

struct u_log_auto_logger {
   u_auto_log_fn *callback;
   void *data;
};

/**
 * Initialize the given logging context.
 */
void
u_log_context_init(struct u_log_context *ctx)
{
   memset(ctx, 0, sizeof(*ctx));
}

/**
 * Free all resources associated with the given logging context.
 *
 * Pages taken from the context via \ref u_log_new_page must be destroyed
 * separately.
 */
void
u_log_context_destroy(struct u_log_context *ctx)
{
   u_log_page_destroy(ctx->cur);
   FREE(ctx->auto_loggers);
   memset(ctx, 0, sizeof(*ctx));
}

/**
 * Add an auto logger.
 *
 * Auto loggers are called each time a chunk is added to the log.
 */
void
u_log_add_auto_logger(struct u_log_context *ctx, u_auto_log_fn *callback,
                      void *data)
{
   struct u_log_auto_logger *new_auto_loggers =
      REALLOC(ctx->auto_loggers,
              sizeof(*new_auto_loggers) * ctx->num_auto_loggers,
              sizeof(*new_auto_loggers) * (ctx->num_auto_loggers + 1));
   if (!new_auto_loggers) {
      fprintf(stderr, "Gallium u_log: out of memory\n");
      return;
   }

   unsigned idx = ctx->num_auto_loggers++;
   ctx->auto_loggers = new_auto_loggers;
   ctx->auto_loggers[idx].callback = callback;
   ctx->auto_loggers[idx].data = data;
}

/**
 * Make sure that auto loggers have run.
 */
void
u_log_flush(struct u_log_context *ctx)
{
   if (!ctx->num_auto_loggers)
      return;

   struct u_log_auto_logger *auto_loggers = ctx->auto_loggers;
   unsigned num_auto_loggers = ctx->num_auto_loggers;

   /* Prevent recursion. */
   ctx->num_auto_loggers = 0;
   ctx->auto_loggers = NULL;

   for (unsigned i = 0; i < num_auto_loggers; ++i)
      auto_loggers[i].callback(auto_loggers[i].data, ctx);

   assert(!ctx->num_auto_loggers);
   ctx->num_auto_loggers = num_auto_loggers;
   ctx->auto_loggers = auto_loggers;
}

static void str_print(void *data, FILE *stream)
{
   fputs((char *)data, stream);
}

static const struct u_log_chunk_type str_chunk_type = {
   .destroy = free,
   .print = str_print,
};

void
u_log_printf(struct u_log_context *ctx, const char *fmt, ...)
{
   va_list va;
   char *str = NULL;

   va_start(va, fmt);
   int ret = util_vasprintf(&str, fmt, va);
   va_end(va);

   if (ret >= 0) {
      u_log_chunk(ctx, &str_chunk_type, str);
   } else {
      fprintf(stderr, "Gallium u_log_printf: out of memory\n");
   }
}

/**
 * Add a custom chunk to the log.
 *
 * type->destroy will be called as soon as \p data is no longer needed.
 */
void
u_log_chunk(struct u_log_context *ctx, const struct u_log_chunk_type *type,
            void *data)
{
   struct u_log_page *page = ctx->cur;

   u_log_flush(ctx);

   if (!page) {
      ctx->cur = CALLOC_STRUCT(u_log_page);
      page = ctx->cur;
      if (!page)
         goto out_of_memory;
   }

   if (page->num_entries >= page->max_entries) {
      unsigned new_max_entries = MAX2(16, page->num_entries * 2);
      struct page_entry *new_entries = REALLOC(page->entries,
                                               page->max_entries * sizeof(*page->entries),
                                               new_max_entries * sizeof(*page->entries));
      if (!new_entries)
         goto out_of_memory;

      page->entries = new_entries;
      page->max_entries = new_max_entries;
   }

   page->entries[page->num_entries].type = type;
   page->entries[page->num_entries].data = data;
   page->num_entries++;
   return;

out_of_memory:
   fprintf(stderr, "Gallium: u_log: out of memory\n");
}

/**
 * Convenience helper that starts a new page and prints the previous one.
 */
void
u_log_new_page_print(struct u_log_context *ctx, FILE *stream)
{
   u_log_flush(ctx);

   if (ctx->cur) {
      u_log_page_print(ctx->cur, stream);
      u_log_page_destroy(ctx->cur);
      ctx->cur = NULL;
   }
}

/**
 * Return the current page from the logging context and start a new one.
 *
 * The caller is responsible for destroying the returned page.
 */
struct u_log_page *
u_log_new_page(struct u_log_context *ctx)
{
   u_log_flush(ctx);

   struct u_log_page *page = ctx->cur;
   ctx->cur = NULL;
   return page;
}

/**
 * Free all data associated with \p page.
 */
void
u_log_page_destroy(struct u_log_page *page)
{
   if (!page)
      return;

   for (unsigned i = 0; i < page->num_entries; ++i) {
      if (page->entries[i].type->destroy)
         page->entries[i].type->destroy(page->entries[i].data);
   }
   FREE(page->entries);
   FREE(page);
}

/**
 * Print the given page to \p stream.
 */
void
u_log_page_print(struct u_log_page *page, FILE *stream)
{
   for (unsigned i = 0; i < page->num_entries; ++i)
      page->entries[i].type->print(page->entries[i].data, stream);
}