yum-archive/TaSTT-Whisper
High-performance GPGPU inference of OpenAI's Whisper automatic speech recognition (ASR) model
git clone https://git.yummers.dev/yum-archive/TaSTT-Whisper
8c4603c
master
1#include "stdafx.h" 2#include "Logger.h" 3#include "../API/iContext.cl.h" 4#include <cstdarg> 5#include <atlstr.h> 6 7namespace 8{ 9wchar_t * formatMessage (HRESULT hr ) 10 { 11wchar_t * err ; 12if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |FORMAT_MESSAGE_FROM_SYSTEM , 13NULL , 14hr , 15MAKELANGID (LANG_NEUTRAL ,SUBLANG_DEFAULT ), 16 (LPTSTR )& err , 170 , 18nullptr ) ) 19return err ; 20return nullptr ; 21 } 22 23class Utf 24 { 25CStringA utf8 ; 26CStringW utf16 ; 27 28void appendError (HRESULT hr ) 29 { 30const wchar_t * err = formatMessage (hr ); 31if (nullptr != err ) 32 { 33utf16 += err ; 34LocalFree ( (HLOCAL )err ); 35utf16 .TrimRight (); 36 } 37else 38utf16 .AppendFormat (L"error code %i (0x%08X)" ,hr ,hr ); 39 } 40 41public : 42const char * const char * pszFormat , std::va_list va ) 43 { 44utf8 .FormatV (pszFormat ,va ); 45return utf8 ; 46 } 47const wchar_t * const wchar_t * pszFormat , std::va_list va ) 48 { 49utf16 .FormatV (pszFormat ,va ); 50return utf16 ; 51 } 52const wchar_t * upcast (const char * message ,int len ) 53 { 54int count = MultiByteToWideChar (CP_UTF8 ,0 ,message ,len ,nullptr ,0 ); 55if (count == 0 ) 56return nullptr ; 57wchar_t * b = utf16 .GetBufferSetLength (len + 1 ); 58count = MultiByteToWideChar (CP_UTF8 ,0 ,message ,len ,b ,len ); 59utf16 .ReleaseBuffer (count ); 60return utf16 ; 61 } 62int utf8Length ()const 63 { 64return utf8 .GetLength (); 65 } 66const wchar_t * printError (HRESULT hr ,const char * pszFormat , std::va_list va ) 67 { 68pszFormat ,va ); 69upcast (utf8 ,utf8 .GetLength () ); 70utf16 += L": " ; 71appendError (hr ); 72return utf16 ; 73 } 74const char * downcast () 75 { 76int count = WideCharToMultiByte (CP_UTF8 ,0 ,utf16 ,utf16 .GetLength (),nullptr ,0 ,nullptr ,nullptr ); 77char * s = utf8 .GetBufferSetLength (count + 1 ); 78count = WideCharToMultiByte (CP_UTF8 ,0 ,utf16 ,utf16 .GetLength (),s ,count ,nullptr ,nullptr ); 79utf8 .ReleaseBufferSetLength (count ); 80return utf8 ; 81 } 82 }; 83 thread_localUtf ts_utf ; 84using Whisper ::eLoggerFlags ; 85 86class Logger :Whisper ::sLoggerSetup 87 { 88inline bool hasFlag (eLoggerFlags bit )const 89 { 90return 0 != ( (uint8_t )flags & (uint8_t )bit ); 91 } 92 93bool useStdError ()const 94 { 95return hasFlag ( eLoggerFlags::UseStandardError ); 96 } 97 98static void writeStdError (Whisper ::eLogLevel lvl ,const char * message ,int len ) 99 { 100const wchar_t * w = ts_utf .upcast (message ,len ); 101if (nullptr != w ) 102fwprintf (stderr ,L"%s\n" ,w ); 103 } 104 105public : 106Logger () 107 { 108memset (this ,0 ,sizeof (Logger ) ); 109 } 110 111bool willLog (Whisper ::eLogLevel lvl )const 112 { 113if ( (uint8_t )lvl > (uint8_t )level ) 114return false; 115if (useStdError () ) 116return true; 117return nullptr != sink ; 118 } 119 120void message (Whisper ::eLogLevel lvl ,const char8_t * pszFormat , std::va_list va )const 121 { 122const char * s = ts_utf .const char * )pszFormat ,va ); 123auto pfn = sink ; 124if (nullptr != pfn ) 125pfn (context ,lvl ,s ); 126if (useStdError () ) 127writeStdError (lvl ,s ,ts_utf .utf8Length () ); 128 } 129void message (Whisper ::eLogLevel lvl ,const wchar_t * pszFormat , std::va_list va )const 130 { 131Utf & u = ts_utf ; 132const wchar_t * w = u .pszFormat ,va ); 133auto pfn = sink ; 134if (nullptr != pfn ) 135pfn (context ,lvl ,u .downcast () ); 136if (useStdError () ) 137fwprintf (stderr ,L"%s\n" ,w ); 138 } 139void message (Whisper ::eLogLevel lvl ,HRESULT hr ,const char * pszFormat , std::va_list va )const 140 { 141if (hasFlag ( eLoggerFlags::SkipFormatMessage ) ) 142 { 143message (lvl , (const char8_t * )pszFormat ,va ); 144return ; 145 } 146Utf & u = ts_utf ; 147const wchar_t * w = ts_utf .printError (hr , (const char * )pszFormat ,va ); 148auto pfn = sink ; 149if (nullptr != pfn ) 150pfn (context ,lvl ,u .downcast () ); 151if (useStdError () ) 152fwprintf (stderr ,L"%s\n" ,w ); 153 } 154 155void operator= (const sLoggerSetup & rsi ) 156 { 157sink = rsi .sink ; 158context = rsi .context ; 159level = rsi .level ; 160flags = rsi .flags ; 161 } 162 }; 163 164static Logger s_logger ; 165} 166 167bool willLogMessage (Whisper ::eLogLevel lvl ) 168{ 169return s_logger .willLog (lvl ); 170} 171 172using Whisper ::eLogLevel ; 173 174#define LOG_MESSAGE_IMPL (lvl ) \ 175 if( !s_logger.willLog( lvl ) ) \ 176 return; \ 177 std::va_list args; \ 178 va_start( args, pszFormat ); \ 179 s_logger.message( lvl, pszFormat, args ); \ 180 va_end( args ); 181 182void logError (const char8_t * pszFormat , ... ) 183{ 184LOG_MESSAGE_IMPL ( eLogLevel::Error ); 185} 186void logError16 (const wchar_t * pszFormat , ... ) 187{ 188LOG_MESSAGE_IMPL ( eLogLevel::Error ); 189} 190void logWarning (const char8_t * pszFormat , ... ) 191{ 192LOG_MESSAGE_IMPL ( eLogLevel::Warning ); 193} 194void logWarning16 (const wchar_t * pszFormat , ... ) 195{ 196LOG_MESSAGE_IMPL ( eLogLevel::Warning ); 197} 198void logInfo (const char8_t * pszFormat , ... ) 199{ 200LOG_MESSAGE_IMPL ( eLogLevel::Info ); 201} 202void logInfo16 (const wchar_t * pszFormat , ... ) 203{ 204LOG_MESSAGE_IMPL ( eLogLevel::Info ); 205} 206void logDebug (const char8_t * pszFormat , ... ) 207{ 208LOG_MESSAGE_IMPL ( eLogLevel::Debug ); 209} 210void logDebug16 (const wchar_t * pszFormat , ... ) 211{ 212LOG_MESSAGE_IMPL ( eLogLevel::Debug ); 213} 214#undef LOG_MESSAGE_IMPL 215 216#define LOG_MESSAGE_IMPL (lvl ) \ 217 if( !s_logger.willLog( lvl ) ) \ 218 return; \ 219 std::va_list args; \ 220 va_start( args, pszFormat ); \ 221 s_logger.message( lvl, hr, (const char*)pszFormat, args ); \ 222 va_end( args ); 223 224void logErrorHr (long hr ,const char8_t * pszFormat , ... ) 225{ 226LOG_MESSAGE_IMPL ( eLogLevel::Error ); 227} 228void logWarningHr (long hr ,const char8_t * pszFormat , ... ) 229{ 230LOG_MESSAGE_IMPL ( eLogLevel::Warning ); 231} 232 233#undef LOG_MESSAGE_IMPL 234 235// DLL entry point 236HRESULT COMLIGHTCALL Whisper ::setupLogger (const sLoggerSetup & setup ) 237{ 238s_logger = setup ; 239return S_OK ; 240}