66#include < cstdio>
77#include < mutex>
88#include < sstream>
9+ #include < fstream>
910#include < thread>
1011#include < vector>
1112
13+ bool use_amd_log = false ;
14+
1215int common_log_verbosity_thold = LOG_DEFAULT_LLAMA;
1316
1417void common_log_set_verbosity_thold (int verbosity) {
@@ -372,7 +375,14 @@ void common_log_free(struct common_log * log) {
372375void common_log_add (struct common_log * log, enum ggml_log_level level, const char * fmt, ...) {
373376 va_list args;
374377 va_start (args, fmt);
375- log->add (level, fmt, args);
378+ if (use_amd_log)
379+ {
380+ amd_log_add (level, fmt, args);
381+ }
382+ else
383+ {
384+ log->add (level, fmt, args);
385+ }
376386 va_end (args);
377387}
378388
@@ -391,3 +401,65 @@ void common_log_set_prefix(struct common_log * log, bool prefix) {
391401void common_log_set_timestamps (struct common_log * log, bool timestamps) {
392402 log->set_timestamps (timestamps);
393403}
404+
405+ std::stringstream amd_server_log;
406+ std::mutex amd_server_log_mutex;
407+
408+ void amd_log_set ()
409+ {
410+ use_amd_log = true ;
411+ }
412+
413+ void amd_log_add (enum ggml_log_level level, const char * text) {
414+ // if (level >= GGML_LOG_LEVEL_DEBUG)
415+ // return;
416+ if (level == GGML_LOG_LEVEL_DEBUG && common_log_verbosity_thold < LOG_DEFAULT_DEBUG) {
417+ return ;
418+ }
419+
420+ std::unique_lock<std::mutex> lock (amd_server_log_mutex);
421+
422+ amd_server_log << text;
423+ }
424+
425+
426+ void amd_log_add (enum ggml_log_level level, const char * fmt, va_list args) {
427+ // if (level >= GGML_LOG_LEVEL_DEBUG)
428+ // return;
429+ if (level == GGML_LOG_LEVEL_DEBUG && common_log_verbosity_thold < LOG_DEFAULT_DEBUG) {
430+ return ;
431+ }
432+
433+ std::unique_lock<std::mutex> lock (amd_server_log_mutex);
434+
435+ std::vector<char > msg;
436+
437+ va_list args_copy;
438+ va_copy (args_copy, args);
439+ const size_t n = vsnprintf (msg.data (), msg.size (), fmt, args);
440+ if (n >= msg.size ()) {
441+ msg.resize (n + 1 );
442+ vsnprintf (msg.data (), msg.size (), fmt, args_copy);
443+ }
444+
445+ amd_server_log << msg.data ();
446+ }
447+
448+ void amd_log_flush_to_file (char * log_file)
449+ {
450+ std::unique_lock<std::mutex> lock (amd_server_log_mutex);
451+
452+ if (NULL != log_file)
453+ {
454+ std::ofstream log (log_file, std::ios::app);
455+ log << amd_server_log.str ();
456+ }
457+
458+ amd_server_log.str (" " );
459+ }
460+
461+ void amd_llama_log_callback (ggml_log_level level, const char * text, void * user_data) {
462+ (void )user_data;
463+ amd_log_add (level, text);
464+ }
465+
0 commit comments