| 1 | package SDL2::log 0.01 { | ||
| 2 | use strictures 2; | ||
| 3 | use SDL2::Utils; | ||
| 4 | use experimental 'signatures'; | ||
| 5 | # | ||
| 6 | use SDL2::stdinc; | ||
| 7 | # | ||
| 8 | define log => [ [ SDL_MAX_LOG_MESSAGE => 4096 ] ]; | ||
| 9 | enum SDL_LogCategory => [ | ||
| 10 | qw[ | ||
| 11 | SDL_LOG_CATEGORY_APPLICATION | ||
| 12 | SDL_LOG_CATEGORY_ERROR | ||
| 13 | SDL_LOG_CATEGORY_ASSERT | ||
| 14 | SDL_LOG_CATEGORY_SYSTEM | ||
| 15 | SDL_LOG_CATEGORY_AUDIO | ||
| 16 | SDL_LOG_CATEGORY_VIDEO | ||
| 17 | SDL_LOG_CATEGORY_RENDER | ||
| 18 | SDL_LOG_CATEGORY_INPUT | ||
| 19 | SDL_LOG_CATEGORY_TEST | ||
| 20 | SDL_LOG_CATEGORY_RESERVED1 | ||
| 21 | SDL_LOG_CATEGORY_RESERVED2 | ||
| 22 | SDL_LOG_CATEGORY_RESERVED3 | ||
| 23 | SDL_LOG_CATEGORY_RESERVED4 | ||
| 24 | SDL_LOG_CATEGORY_RESERVED5 | ||
| 25 | SDL_LOG_CATEGORY_RESERVED6 | ||
| 26 | SDL_LOG_CATEGORY_RESERVED7 | ||
| 27 | SDL_LOG_CATEGORY_RESERVED8 | ||
| 28 | SDL_LOG_CATEGORY_RESERVED9 | ||
| 29 | SDL_LOG_CATEGORY_RESERVED10 | ||
| 30 | SDL_LOG_CATEGORY_CUSTOM] | ||
| 31 | ], | ||
| 32 | SDL_LogPriority => [ | ||
| 33 | [ SDL_LOG_PRIORITY_VERBOSE => 1 ], qw[SDL_LOG_PRIORITY_DEBUG | ||
| 34 | SDL_LOG_PRIORITY_INFO | ||
| 35 | SDL_LOG_PRIORITY_WARN | ||
| 36 | SDL_LOG_PRIORITY_ERROR | ||
| 37 | SDL_LOG_PRIORITY_CRITICAL | ||
| 38 | SDL_NUM_LOG_PRIORITIES] | ||
| 39 | ]; | ||
| 40 | attach log => { | ||
| 41 | SDL_LogSetAllPriority => [ ['SDL_LogPriority'] ], | ||
| 42 | SDL_LogSetPriority => [ [ 'int', 'SDL_LogPriority' ] ], | ||
| 43 | SDL_LogGetPriority => [ ['int'], 'SDL_LogPriority' ], | ||
| 44 | SDL_LogResetPriorities => [ [] ], | ||
| 45 | }; | ||
| 46 | ffi->type( '(opaque,int,int,string)->void' => 'SDL_LogOutputFunction' ); | ||
| 47 | define log => [ | ||
| 48 | [ SDL_Log => sub ( $format, @args ) { | ||
| 49 | my @types = SDL2::Utils::_tokenize_out( $format, 0 ); | ||
| 50 | ffi->function( SDL_Log => ['string'] => [@types] )->call( $format, @args ); | ||
| 51 | } | ||
| 52 | ], | ||
| 53 | [ SDL_LogVerbose => sub ( $category, $format, @args ) { | ||
| 54 | my @types = SDL2::Utils::_tokenize_out( $format, 0 ); | ||
| 55 | ffi->function( SDL_LogVerbose => [ 'int', 'string' ] => [@types] ) | ||
| 56 | ->call( $category, $format, @args ); | ||
| 57 | } | ||
| 58 | ], | ||
| 59 | [ SDL_LogDebug => sub ( $category, $format, @args ) { | ||
| 60 | my @types = SDL2::Utils::_tokenize_out( $format, 0 ); | ||
| 61 | ffi->function( SDL_LogDebug => [ 'int', 'string' ] => [@types] ) | ||
| 62 | ->call( $category, $format, @args ); | ||
| 63 | } | ||
| 64 | ], | ||
| 65 | [ SDL_LogInfo => sub ( $category, $format, @args ) { | ||
| 66 | my @types = SDL2::Utils::_tokenize_out( $format, 0 ); | ||
| 67 | ffi->function( SDL_LogInfo => [ 'int', 'string' ] => [@types] ) | ||
| 68 | ->call( $category, $format, @args ); | ||
| 69 | } | ||
| 70 | ], | ||
| 71 | [ SDL_LogWarn => sub ( $category, $format, @args ) { | ||
| 72 | my @types = SDL2::Utils::_tokenize_out( $format, 0 ); | ||
| 73 | ffi->function( SDL_LogWarn => [ 'int', 'string' ] => [@types] ) | ||
| 74 | ->call( $category, $format, @args ); | ||
| 75 | } | ||
| 76 | ], | ||
| 77 | [ SDL_LogError => sub ( $category, $format, @args ) { | ||
| 78 | my @types = SDL2::Utils::_tokenize_out( $format, 0 ); | ||
| 79 | ffi->function( SDL_LogError => [ 'int', 'string' ] => [@types] ) | ||
| 80 | ->call( $category, $format, @args ); | ||
| 81 | } | ||
| 82 | ], | ||
| 83 | [ SDL_LogCritical => sub ( $category, $format, @args ) { | ||
| 84 | my @types = SDL2::Utils::_tokenize_out( $format, 0 ); | ||
| 85 | ffi->function( SDL_LogCritical => [ 'int', 'string' ] => [@types] ) | ||
| 86 | ->call( $category, $format, @args ); | ||
| 87 | } | ||
| 88 | ], | ||
| 89 | [ SDL_LogMessage => sub ( $category, $priority, $format, @args ) { | ||
| 90 | my @types = SDL2::Utils::_tokenize_out( $format, 0 ); | ||
| 91 | ffi->function( | ||
| 92 | SDL_LogMessage => [ 'int', 'SDL_LogPriority', 'string' ] => [@types] ) | ||
| 93 | ->call( $category, $priority, $format, @args ); | ||
| 94 | } | ||
| 95 | ], | ||
| 96 | [ SDL_LogMessageV => | ||
| 97 | sub ( $category, $priority, $format, @args ) { SDL2::FFI::SDL_LogMessage(@_) } | ||
| 98 | ], | ||
| 99 | ]; | ||
| 100 | attach log => { | ||
| 101 | SDL_LogGetOutputFunction => [ | ||
| 102 | [ 'opaque*', 'opaque*' ] => sub ( $inner, $callback, $userdata ) { | ||
| 103 | $inner->( $callback, $userdata ); | ||
| 104 | $$callback = ffi->function( $$callback => [ 'opaque', 'int', 'int', 'string' ] ) | ||
| 105 | ; # Unwrap it | ||
| 106 | } | ||
| 107 | ], | ||
| 108 | SDL_LogSetOutputFunction => [ | ||
| 109 | [ 'SDL_LogOutputFunction', 'opaque' ] => sub ( $inner, $callback, $userdata = () ) { | ||
| 110 | my $closure = ffi->closure( | ||
| 111 | sub ( $ud, $cat, $pri, $msg ) { $callback->( $userdata, $cat, $pri, $msg ) } ); | ||
| 112 | $closure->sticky; | ||
| 113 | $inner->( $closure, $userdata ); | ||
| 114 | } | ||
| 115 | ] | ||
| 116 | }; | ||
| 117 | |||
| 118 | =encoding utf-8 | ||
| 119 | |||
| 120 | =head1 NAME | ||
| 121 | |||
| 122 | SDL2::log - Simple Log Messages with Categories and Priorities | ||
| 123 | |||
| 124 | =head1 SYNOPSIS | ||
| 125 | |||
| 126 | use SDL2 qw[:log]; | ||
| 127 | |||
| 128 | =head1 DESCRIPTION | ||
| 129 | |||
| 130 | By default, logs are quiet but if you're debugging SDL you might want: | ||
| 131 | |||
| 132 | SDL_LogSetAllPriority( SDL_LOG_PRIORITY_WARN ); | ||
| 133 | |||
| 134 | Here's where the messages go on different platforms: | ||
| 135 | |||
| 136 | =over | ||
| 137 | |||
| 138 | =item Windows: debug output stream | ||
| 139 | |||
| 140 | =item Android: log output | ||
| 141 | |||
| 142 | =item Others: standard error output (stderr) | ||
| 143 | |||
| 144 | =back | ||
| 145 | |||
| 146 | =head1 Functions | ||
| 147 | |||
| 148 | These functions may be imported by name or with the C<:log> tag. | ||
| 149 | |||
| 150 | =head2 C<SDL_LogSetAllPriority( ... )> | ||
| 151 | |||
| 152 | Set the priority of all log categories. | ||
| 153 | |||
| 154 | Expected parameters include: | ||
| 155 | |||
| 156 | =over | ||
| 157 | |||
| 158 | =item C<priority> - the C<SDL_LogPriority> to assign | ||
| 159 | |||
| 160 | =back | ||
| 161 | |||
| 162 | =head2 C<SDL_LogSetPriority( ... )> | ||
| 163 | |||
| 164 | Set the priority of a particular log category. | ||
| 165 | |||
| 166 | Expected parameters include: | ||
| 167 | |||
| 168 | =over | ||
| 169 | |||
| 170 | =item C<category> - the category to assign a priority to | ||
| 171 | |||
| 172 | =item C<priority> - the C<SDL_LogPriority> to assign | ||
| 173 | |||
| 174 | =back | ||
| 175 | |||
| 176 | =head2 C<SDL_LogGetPriority( ... )> | ||
| 177 | |||
| 178 | Get the priority of a particular log category. | ||
| 179 | |||
| 180 | Expected parameters include: | ||
| 181 | |||
| 182 | =over | ||
| 183 | |||
| 184 | =item C<category> - the category to query | ||
| 185 | |||
| 186 | =back | ||
| 187 | |||
| 188 | Returns the C<SDL_LogPriority> for the requested category. | ||
| 189 | |||
| 190 | =head2 C<SDL_LogResetPriorities( )> | ||
| 191 | |||
| 192 | Reset all priorities to default. | ||
| 193 | |||
| 194 | This is called by C<SDL_Quit( )>. | ||
| 195 | |||
| 196 | =head2 C<SDL_Log( ... )> | ||
| 197 | |||
| 198 | Log a message with C<SDL_LOG_CATEGORY_APPLICATION> and | ||
| 199 | C<SDL_LOG_PRIORITY_INFO>. | ||
| 200 | |||
| 201 | Expected parameters include: | ||
| 202 | |||
| 203 | =over | ||
| 204 | |||
| 205 | =item C<fmt> - a C<printf( ... )> style message format string | ||
| 206 | |||
| 207 | =item C<...> - additional parameters matching C<%> tokens in the C<fmt> string, if any | ||
| 208 | |||
| 209 | =back | ||
| 210 | |||
| 211 | =head2 C<SDL_LogVerbose( ... )> | ||
| 212 | |||
| 213 | Log a message with C<SDL_LOG_PRIORITY_VERBOSE>. | ||
| 214 | |||
| 215 | Expected parameters include: | ||
| 216 | |||
| 217 | =over | ||
| 218 | |||
| 219 | =item C<category> - the category of the message | ||
| 220 | |||
| 221 | =item C<fmt> - a C<printf( ... )> style message format string | ||
| 222 | |||
| 223 | =item C<...> - additional parameters matching C<%> tokens in the C<fmt> string, if any | ||
| 224 | |||
| 225 | =back | ||
| 226 | |||
| 227 | =head2 C<SDL_LogDebug( ... )> | ||
| 228 | |||
| 229 | Log a message with C<SDL_LOG_PRIORITY_DEBUG>. | ||
| 230 | |||
| 231 | Expected parameters include: | ||
| 232 | |||
| 233 | =over | ||
| 234 | |||
| 235 | =item C<category> - the category of the message | ||
| 236 | |||
| 237 | =item C<fmt> - a C<printf( ... )> style message format string | ||
| 238 | |||
| 239 | =item C<...> - additional parameters matching C<%> tokens in the C<fmt> string, if any | ||
| 240 | |||
| 241 | =back | ||
| 242 | |||
| 243 | =head2 C<SDL_LogInfo( ... )> | ||
| 244 | |||
| 245 | Log a message with C<SDL_LOG_PRIORITY_INFO>. | ||
| 246 | |||
| 247 | Expected parameters include: | ||
| 248 | |||
| 249 | =over | ||
| 250 | |||
| 251 | =item C<category> - the category of the message | ||
| 252 | |||
| 253 | =item C<fmt> - a C<printf( ... )> style message format string | ||
| 254 | |||
| 255 | =item C<...> - additional parameters matching C<%> tokens in the C<fmt> string, if any | ||
| 256 | |||
| 257 | =back | ||
| 258 | |||
| 259 | =head2 C<SDL_LogWarn( ... )> | ||
| 260 | |||
| 261 | Log a message with C<SDL_LOG_PRIORITY_WARN>. | ||
| 262 | |||
| 263 | Expected parameters include: | ||
| 264 | |||
| 265 | =over | ||
| 266 | |||
| 267 | =item C<category> - the category of the message | ||
| 268 | |||
| 269 | =item C<fmt> - a C<printf( ... )> style message format string | ||
| 270 | |||
| 271 | =item C<...> - additional parameters matching C<%> tokens in the C<fmt> string, if any | ||
| 272 | |||
| 273 | =back | ||
| 274 | |||
| 275 | =head2 C<SDL_LogError( ... )> | ||
| 276 | |||
| 277 | Log a message with C<SDL_LOG_PRIORITY_ERROR>. | ||
| 278 | |||
| 279 | Expected parameters include: | ||
| 280 | |||
| 281 | =over | ||
| 282 | |||
| 283 | =item C<category> - the category of the message | ||
| 284 | |||
| 285 | =item C<fmt> - a C<printf( ... )> style message format string | ||
| 286 | |||
| 287 | =item C<...> - additional parameters matching C<%> tokens in the C<fmt> string, if any | ||
| 288 | |||
| 289 | =back | ||
| 290 | |||
| 291 | =head2 C<SDL_LogCritical( ... )> | ||
| 292 | |||
| 293 | Log a message with C<SDL_LOG_PRIORITY_CRITICAL>. | ||
| 294 | |||
| 295 | Expected parameters include: | ||
| 296 | |||
| 297 | =over | ||
| 298 | |||
| 299 | =item C<category> - the category of the message | ||
| 300 | |||
| 301 | =item C<fmt> - a C<printf( ... )> style message format string | ||
| 302 | |||
| 303 | =item C<...> - additional parameters matching C<%> tokens in the C<fmt> string, if any | ||
| 304 | |||
| 305 | =back | ||
| 306 | |||
| 307 | =head2 C<SDL_LogMessage( ... )> | ||
| 308 | |||
| 309 | Log a message with the specified category and priority. | ||
| 310 | |||
| 311 | Expected parameters include: | ||
| 312 | |||
| 313 | =over | ||
| 314 | |||
| 315 | =item C<category> - the category of the message | ||
| 316 | |||
| 317 | =item C<priority> - the priority of the message | ||
| 318 | |||
| 319 | =item C<fmt> - a C<printf( ... )> style message format string | ||
| 320 | |||
| 321 | =item C<...> - additional parameters matching C<%> tokens in the C<fmt> string, if any | ||
| 322 | |||
| 323 | =back | ||
| 324 | |||
| 325 | =head2 C<SDL_LogMessageV( ... )> | ||
| 326 | |||
| 327 | Expected parameters include: | ||
| 328 | |||
| 329 | =over | ||
| 330 | |||
| 331 | =item C<category> - the category of the message | ||
| 332 | |||
| 333 | =item C<priority> - the priority of the message | ||
| 334 | |||
| 335 | =item C<fmt> - a C<printf( ... )> style message format string | ||
| 336 | |||
| 337 | =item C<...> - additional parameters matching C<%> tokens in the C<fmt> string, if any | ||
| 338 | |||
| 339 | =back | ||
| 340 | |||
| 341 | =head2 C<SDL_LogGetOutputFunction( ... )> | ||
| 342 | |||
| 343 | Get the current log output function. | ||
| 344 | |||
| 345 | Expected parameters include: | ||
| 346 | |||
| 347 | =over | ||
| 348 | |||
| 349 | =item C<callback> - pointer which will be filled in with the current log callback | ||
| 350 | |||
| 351 | =item C<userdata> - a pointer which will be filled in with the pointer that is passed to C<callback> | ||
| 352 | |||
| 353 | =back | ||
| 354 | |||
| 355 | =head2 C<SDL_LogSetOutputFunction( ... )> | ||
| 356 | |||
| 357 | Replace the default log output function with one of your own. | ||
| 358 | |||
| 359 | Expected parameters include: | ||
| 360 | |||
| 361 | =over | ||
| 362 | |||
| 363 | =item C<callback> - an C<SDL_LogOutputFunction> to call instead of the default | ||
| 364 | |||
| 365 | =item C<userdata> - a pointer that is passed to C<callback> | ||
| 366 | |||
| 367 | =back | ||
| 368 | |||
| 369 | =head2 Defined Values and Enumerations | ||
| 370 | |||
| 371 | These might be imported with the given tag, by name, or with the <:log> tag. | ||
| 372 | |||
| 373 | =head2 C<SDL_MAX_LOG_MESSAGE> | ||
| 374 | |||
| 375 | The maximum size of a log message. | ||
| 376 | |||
| 377 | Messages longer than the maximum size will be truncated. | ||
| 378 | |||
| 379 | =head2 C<SDL_LogCategory> | ||
| 380 | |||
| 381 | The predefined log categories | ||
| 382 | |||
| 383 | By default the application category is enabled at the INFO level, the assert | ||
| 384 | category is enabled at the WARN level, test is enabled at the VERBOSE level and | ||
| 385 | all other categories are enabled at the C<CRITICAL> level. | ||
| 386 | |||
| 387 | =over | ||
| 388 | |||
| 389 | =item C<SDL_LOG_CATEGORY_APPLICATION> | ||
| 390 | |||
| 391 | =item C<SDL_LOG_CATEGORY_ERROR> | ||
| 392 | |||
| 393 | =item C<SDL_LOG_CATEGORY_ASSERT> | ||
| 394 | |||
| 395 | =item C<SDL_LOG_CATEGORY_SYSTEM> | ||
| 396 | |||
| 397 | =item C<SDL_LOG_CATEGORY_AUDIO> | ||
| 398 | |||
| 399 | =item C<SDL_LOG_CATEGORY_VIDEO> | ||
| 400 | |||
| 401 | =item C<SDL_LOG_CATEGORY_RENDER> | ||
| 402 | |||
| 403 | =item C<SDL_LOG_CATEGORY_INPUT> | ||
| 404 | |||
| 405 | =item C<SDL_LOG_CATEGORY_TEST> | ||
| 406 | |||
| 407 | =item C<SDL_LOG_CATEGORY_RESERVED1> - These are reserved for future SDL library use | ||
| 408 | |||
| 409 | =item C<SDL_LOG_CATEGORY_RESERVED2> | ||
| 410 | |||
| 411 | =item C<SDL_LOG_CATEGORY_RESERVED3> | ||
| 412 | |||
| 413 | =item C<SDL_LOG_CATEGORY_RESERVED4> | ||
| 414 | |||
| 415 | =item C<SDL_LOG_CATEGORY_RESERVED5> | ||
| 416 | |||
| 417 | =item C<SDL_LOG_CATEGORY_RESERVED6> | ||
| 418 | |||
| 419 | =item C<SDL_LOG_CATEGORY_RESERVED7> | ||
| 420 | |||
| 421 | =item C<SDL_LOG_CATEGORY_RESERVED8> | ||
| 422 | |||
| 423 | =item C<SDL_LOG_CATEGORY_RESERVED9> | ||
| 424 | |||
| 425 | =item C<SDL_LOG_CATEGORY_RESERVED10> | ||
| 426 | |||
| 427 | =item C<SDL_LOG_CATEGORY_CUSTOM> | ||
| 428 | |||
| 429 | Beyond this point is reserved for application use, e.g. | ||
| 430 | |||
| 431 | enum { | ||
| 432 | MYAPP_CATEGORY_AWESOME1 = SDL_LOG_CATEGORY_CUSTOM, | ||
| 433 | MYAPP_CATEGORY_AWESOME2, | ||
| 434 | MYAPP_CATEGORY_AWESOME3, | ||
| 435 | ... | ||
| 436 | }; | ||
| 437 | |||
| 438 | =back | ||
| 439 | |||
| 440 | =head2 C<SDL_LogPriority> | ||
| 441 | |||
| 442 | The predefined log priorities. | ||
| 443 | |||
| 444 | =over | ||
| 445 | |||
| 446 | =item C<SDL_LOG_PRIORITY_VERBOSE> | ||
| 447 | |||
| 448 | =item C<SDL_LOG_PRIORITY_DEBUG> | ||
| 449 | |||
| 450 | =item C<SDL_LOG_PRIORITY_INFO> | ||
| 451 | |||
| 452 | =item C<SDL_LOG_PRIORITY_WARN> | ||
| 453 | |||
| 454 | =item C<SDL_LOG_PRIORITY_ERROR> | ||
| 455 | |||
| 456 | =item C<SDL_LOG_PRIORITY_CRITICAL> | ||
| 457 | |||
| 458 | =item C<SDL_NUM_LOG_PRIORITIES> | ||
| 459 | |||
| 460 | =back | ||
| 461 | |||
| 462 | =head2 C<SDL_LogOutputFunction> | ||
| 463 | |||
| 464 | The prototype for the log output callback function. | ||
| 465 | |||
| 466 | This function is called by SDL when there is new text to be logged. | ||
| 467 | |||
| 468 | Parameters to expect include: | ||
| 469 | |||
| 470 | =over | ||
| 471 | |||
| 472 | =item C<userdata> - what was passed as C<userdata> to L<< C<SDL_LogSetOutputFunction( ... )>|/C<SDL_LogSetOutputFunction( ... )> >> | ||
| 473 | |||
| 474 | =item C<category> - the category of the message | ||
| 475 | |||
| 476 | =item C<priority> - the priority of the message | ||
| 477 | |||
| 478 | =item C<message> - the message being output | ||
| 479 | |||
| 480 | =back | ||
| 481 | |||
| 482 | =head1 LICENSE | ||
| 483 | |||
| 484 | Copyright (C) Sanko Robinson. | ||
| 485 | |||
| 486 | This library is free software; you can redistribute it and/or modify it under | ||
| 487 | the terms found in the Artistic License 2. Other copyrights, terms, and | ||
| 488 | conditions may apply to data transmitted through this module. | ||
| 489 | |||
| 490 | =head1 AUTHOR | ||
| 491 | |||
| 492 | Sanko Robinson E<lt>sanko@cpan.orgE<gt> | ||
| 493 | |||
| 494 | =begin stopwords | ||
| 495 | |||
| 496 | =end stopwords | ||
| 497 | |||
| 498 | =cut | ||
| 499 | |||
| 500 | }; | ||
| 501 | 1; |