1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright 2017 Advanced Micro Devices, Inc. 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub 8bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom 9bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, 19bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 20bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 21bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include "u_log.h" 25bf215546Sopenharmony_ci 26bf215546Sopenharmony_ci#include "util/u_memory.h" 27bf215546Sopenharmony_ci#include "util/u_string.h" 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_cistruct page_entry { 30bf215546Sopenharmony_ci const struct u_log_chunk_type *type; 31bf215546Sopenharmony_ci void *data; 32bf215546Sopenharmony_ci}; 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_cistruct u_log_page { 35bf215546Sopenharmony_ci struct page_entry *entries; 36bf215546Sopenharmony_ci unsigned num_entries; 37bf215546Sopenharmony_ci unsigned max_entries; 38bf215546Sopenharmony_ci}; 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_cistruct u_log_auto_logger { 41bf215546Sopenharmony_ci u_auto_log_fn *callback; 42bf215546Sopenharmony_ci void *data; 43bf215546Sopenharmony_ci}; 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_ci/** 46bf215546Sopenharmony_ci * Initialize the given logging context. 47bf215546Sopenharmony_ci */ 48bf215546Sopenharmony_civoid 49bf215546Sopenharmony_ciu_log_context_init(struct u_log_context *ctx) 50bf215546Sopenharmony_ci{ 51bf215546Sopenharmony_ci memset(ctx, 0, sizeof(*ctx)); 52bf215546Sopenharmony_ci} 53bf215546Sopenharmony_ci 54bf215546Sopenharmony_ci/** 55bf215546Sopenharmony_ci * Free all resources associated with the given logging context. 56bf215546Sopenharmony_ci * 57bf215546Sopenharmony_ci * Pages taken from the context via \ref u_log_new_page must be destroyed 58bf215546Sopenharmony_ci * separately. 59bf215546Sopenharmony_ci */ 60bf215546Sopenharmony_civoid 61bf215546Sopenharmony_ciu_log_context_destroy(struct u_log_context *ctx) 62bf215546Sopenharmony_ci{ 63bf215546Sopenharmony_ci u_log_page_destroy(ctx->cur); 64bf215546Sopenharmony_ci FREE(ctx->auto_loggers); 65bf215546Sopenharmony_ci memset(ctx, 0, sizeof(*ctx)); 66bf215546Sopenharmony_ci} 67bf215546Sopenharmony_ci 68bf215546Sopenharmony_ci/** 69bf215546Sopenharmony_ci * Add an auto logger. 70bf215546Sopenharmony_ci * 71bf215546Sopenharmony_ci * Auto loggers are called each time a chunk is added to the log. 72bf215546Sopenharmony_ci */ 73bf215546Sopenharmony_civoid 74bf215546Sopenharmony_ciu_log_add_auto_logger(struct u_log_context *ctx, u_auto_log_fn *callback, 75bf215546Sopenharmony_ci void *data) 76bf215546Sopenharmony_ci{ 77bf215546Sopenharmony_ci struct u_log_auto_logger *new_auto_loggers = 78bf215546Sopenharmony_ci REALLOC(ctx->auto_loggers, 79bf215546Sopenharmony_ci sizeof(*new_auto_loggers) * ctx->num_auto_loggers, 80bf215546Sopenharmony_ci sizeof(*new_auto_loggers) * (ctx->num_auto_loggers + 1)); 81bf215546Sopenharmony_ci if (!new_auto_loggers) { 82bf215546Sopenharmony_ci fprintf(stderr, "Gallium u_log: out of memory\n"); 83bf215546Sopenharmony_ci return; 84bf215546Sopenharmony_ci } 85bf215546Sopenharmony_ci 86bf215546Sopenharmony_ci unsigned idx = ctx->num_auto_loggers++; 87bf215546Sopenharmony_ci ctx->auto_loggers = new_auto_loggers; 88bf215546Sopenharmony_ci ctx->auto_loggers[idx].callback = callback; 89bf215546Sopenharmony_ci ctx->auto_loggers[idx].data = data; 90bf215546Sopenharmony_ci} 91bf215546Sopenharmony_ci 92bf215546Sopenharmony_ci/** 93bf215546Sopenharmony_ci * Make sure that auto loggers have run. 94bf215546Sopenharmony_ci */ 95bf215546Sopenharmony_civoid 96bf215546Sopenharmony_ciu_log_flush(struct u_log_context *ctx) 97bf215546Sopenharmony_ci{ 98bf215546Sopenharmony_ci if (!ctx->num_auto_loggers) 99bf215546Sopenharmony_ci return; 100bf215546Sopenharmony_ci 101bf215546Sopenharmony_ci struct u_log_auto_logger *auto_loggers = ctx->auto_loggers; 102bf215546Sopenharmony_ci unsigned num_auto_loggers = ctx->num_auto_loggers; 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_ci /* Prevent recursion. */ 105bf215546Sopenharmony_ci ctx->num_auto_loggers = 0; 106bf215546Sopenharmony_ci ctx->auto_loggers = NULL; 107bf215546Sopenharmony_ci 108bf215546Sopenharmony_ci for (unsigned i = 0; i < num_auto_loggers; ++i) 109bf215546Sopenharmony_ci auto_loggers[i].callback(auto_loggers[i].data, ctx); 110bf215546Sopenharmony_ci 111bf215546Sopenharmony_ci assert(!ctx->num_auto_loggers); 112bf215546Sopenharmony_ci ctx->num_auto_loggers = num_auto_loggers; 113bf215546Sopenharmony_ci ctx->auto_loggers = auto_loggers; 114bf215546Sopenharmony_ci} 115bf215546Sopenharmony_ci 116bf215546Sopenharmony_cistatic void str_print(void *data, FILE *stream) 117bf215546Sopenharmony_ci{ 118bf215546Sopenharmony_ci fputs((char *)data, stream); 119bf215546Sopenharmony_ci} 120bf215546Sopenharmony_ci 121bf215546Sopenharmony_cistatic const struct u_log_chunk_type str_chunk_type = { 122bf215546Sopenharmony_ci .destroy = free, 123bf215546Sopenharmony_ci .print = str_print, 124bf215546Sopenharmony_ci}; 125bf215546Sopenharmony_ci 126bf215546Sopenharmony_civoid 127bf215546Sopenharmony_ciu_log_printf(struct u_log_context *ctx, const char *fmt, ...) 128bf215546Sopenharmony_ci{ 129bf215546Sopenharmony_ci va_list va; 130bf215546Sopenharmony_ci char *str = NULL; 131bf215546Sopenharmony_ci 132bf215546Sopenharmony_ci va_start(va, fmt); 133bf215546Sopenharmony_ci int ret = vasprintf(&str, fmt, va); 134bf215546Sopenharmony_ci va_end(va); 135bf215546Sopenharmony_ci 136bf215546Sopenharmony_ci if (ret >= 0) { 137bf215546Sopenharmony_ci u_log_chunk(ctx, &str_chunk_type, str); 138bf215546Sopenharmony_ci } else { 139bf215546Sopenharmony_ci fprintf(stderr, "Gallium u_log_printf: out of memory\n"); 140bf215546Sopenharmony_ci } 141bf215546Sopenharmony_ci} 142bf215546Sopenharmony_ci 143bf215546Sopenharmony_ci/** 144bf215546Sopenharmony_ci * Add a custom chunk to the log. 145bf215546Sopenharmony_ci * 146bf215546Sopenharmony_ci * type->destroy will be called as soon as \p data is no longer needed. 147bf215546Sopenharmony_ci */ 148bf215546Sopenharmony_civoid 149bf215546Sopenharmony_ciu_log_chunk(struct u_log_context *ctx, const struct u_log_chunk_type *type, 150bf215546Sopenharmony_ci void *data) 151bf215546Sopenharmony_ci{ 152bf215546Sopenharmony_ci struct u_log_page *page = ctx->cur; 153bf215546Sopenharmony_ci 154bf215546Sopenharmony_ci u_log_flush(ctx); 155bf215546Sopenharmony_ci 156bf215546Sopenharmony_ci if (!page) { 157bf215546Sopenharmony_ci ctx->cur = CALLOC_STRUCT(u_log_page); 158bf215546Sopenharmony_ci page = ctx->cur; 159bf215546Sopenharmony_ci if (!page) 160bf215546Sopenharmony_ci goto out_of_memory; 161bf215546Sopenharmony_ci } 162bf215546Sopenharmony_ci 163bf215546Sopenharmony_ci if (page->num_entries >= page->max_entries) { 164bf215546Sopenharmony_ci unsigned new_max_entries = MAX2(16, page->num_entries * 2); 165bf215546Sopenharmony_ci struct page_entry *new_entries = REALLOC(page->entries, 166bf215546Sopenharmony_ci page->max_entries * sizeof(*page->entries), 167bf215546Sopenharmony_ci new_max_entries * sizeof(*page->entries)); 168bf215546Sopenharmony_ci if (!new_entries) 169bf215546Sopenharmony_ci goto out_of_memory; 170bf215546Sopenharmony_ci 171bf215546Sopenharmony_ci page->entries = new_entries; 172bf215546Sopenharmony_ci page->max_entries = new_max_entries; 173bf215546Sopenharmony_ci } 174bf215546Sopenharmony_ci 175bf215546Sopenharmony_ci page->entries[page->num_entries].type = type; 176bf215546Sopenharmony_ci page->entries[page->num_entries].data = data; 177bf215546Sopenharmony_ci page->num_entries++; 178bf215546Sopenharmony_ci return; 179bf215546Sopenharmony_ci 180bf215546Sopenharmony_ciout_of_memory: 181bf215546Sopenharmony_ci fprintf(stderr, "Gallium: u_log: out of memory\n"); 182bf215546Sopenharmony_ci} 183bf215546Sopenharmony_ci 184bf215546Sopenharmony_ci/** 185bf215546Sopenharmony_ci * Convenience helper that starts a new page and prints the previous one. 186bf215546Sopenharmony_ci */ 187bf215546Sopenharmony_civoid 188bf215546Sopenharmony_ciu_log_new_page_print(struct u_log_context *ctx, FILE *stream) 189bf215546Sopenharmony_ci{ 190bf215546Sopenharmony_ci u_log_flush(ctx); 191bf215546Sopenharmony_ci 192bf215546Sopenharmony_ci if (ctx->cur) { 193bf215546Sopenharmony_ci u_log_page_print(ctx->cur, stream); 194bf215546Sopenharmony_ci u_log_page_destroy(ctx->cur); 195bf215546Sopenharmony_ci ctx->cur = NULL; 196bf215546Sopenharmony_ci } 197bf215546Sopenharmony_ci} 198bf215546Sopenharmony_ci 199bf215546Sopenharmony_ci/** 200bf215546Sopenharmony_ci * Return the current page from the logging context and start a new one. 201bf215546Sopenharmony_ci * 202bf215546Sopenharmony_ci * The caller is responsible for destroying the returned page. 203bf215546Sopenharmony_ci */ 204bf215546Sopenharmony_cistruct u_log_page * 205bf215546Sopenharmony_ciu_log_new_page(struct u_log_context *ctx) 206bf215546Sopenharmony_ci{ 207bf215546Sopenharmony_ci u_log_flush(ctx); 208bf215546Sopenharmony_ci 209bf215546Sopenharmony_ci struct u_log_page *page = ctx->cur; 210bf215546Sopenharmony_ci ctx->cur = NULL; 211bf215546Sopenharmony_ci return page; 212bf215546Sopenharmony_ci} 213bf215546Sopenharmony_ci 214bf215546Sopenharmony_ci/** 215bf215546Sopenharmony_ci * Free all data associated with \p page. 216bf215546Sopenharmony_ci */ 217bf215546Sopenharmony_civoid 218bf215546Sopenharmony_ciu_log_page_destroy(struct u_log_page *page) 219bf215546Sopenharmony_ci{ 220bf215546Sopenharmony_ci if (!page) 221bf215546Sopenharmony_ci return; 222bf215546Sopenharmony_ci 223bf215546Sopenharmony_ci for (unsigned i = 0; i < page->num_entries; ++i) { 224bf215546Sopenharmony_ci if (page->entries[i].type->destroy) 225bf215546Sopenharmony_ci page->entries[i].type->destroy(page->entries[i].data); 226bf215546Sopenharmony_ci } 227bf215546Sopenharmony_ci FREE(page->entries); 228bf215546Sopenharmony_ci FREE(page); 229bf215546Sopenharmony_ci} 230bf215546Sopenharmony_ci 231bf215546Sopenharmony_ci/** 232bf215546Sopenharmony_ci * Print the given page to \p stream. 233bf215546Sopenharmony_ci */ 234bf215546Sopenharmony_civoid 235bf215546Sopenharmony_ciu_log_page_print(struct u_log_page *page, FILE *stream) 236bf215546Sopenharmony_ci{ 237bf215546Sopenharmony_ci for (unsigned i = 0; i < page->num_entries; ++i) 238bf215546Sopenharmony_ci page->entries[i].type->print(page->entries[i].data, stream); 239bf215546Sopenharmony_ci} 240