Advertisement
Guest User

Untitled

a guest
Jul 15th, 2022
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.15 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "api_types.h"
  4.  
  5. // Provides functions for logging errors and informational messages.
  6. //
  7. // Be wary of logging too much. As developers it is often tempting to puts in lots of informational
  8. // messages such as `Initializing sound system.` or `Connecting to server.`, but each message you
  9. // put in will demand some attention from the user. If there are a lot of messages of little value,
  10. // the user will start ignoring the log altogether.
  11. //
  12. // Only use a log message when it provides clear value to the user.
  13. //
  14. // If you have messages that *sometimes* are useful, but sometimes not, such as network diagnostics,
  15. // consider hiding them behind a `verbose` flag so that users can explicitly enable them when
  16. // needed.
  17. //
  18. // The logging system does not have support for warnings. This is by design. Warnings can be an
  19. // especially bad form of log spew. Warnings scream *I need to be payed attention to*, but since
  20. // they are just "warnings", they may not indicate real errors. The result is often that you end up
  21. // with a log full of "false positives". This is a great way to train users to stop paying attention
  22. // to the log completely, potentially missing serious errors.
  23. //
  24. // Our philosophy is that something is either an *error*, in which it the user should fix it so that
  25. // the error message disappears. Or it is *not an error*, in which case the log shouldn't print any
  26. // ominous warnings about it.
  27. //
  28. // In situations where you might be tempted to print a warning, we suggest creating a customized
  29. // tool instead. For example, you might be tempted to print a warning for models with a texel
  30. // density > 1 texel / 0.1 mm. Such a high texture density often indicates a performance problem
  31. // where an artist have assigned a high-resolution texture to a small object. However, it is not
  32. // necessary an error. Perhaps the object is a loupe that is positioned really close to the user's
  33. // eye and needs the high density. Also, you have to pick an arbitrary cutoff point for when
  34. // warnings should be generated.
  35. //
  36. // A better approach is to create a specialized tool for examining and optimizing texel densities.
  37. // For example, it could show all models in the project, sorted by texel density. It could also
  38. // include a special viewport mode where models are colored by texel/pixel ratio. This way, a TA
  39. // could examine and address texel density issues with much better precision. One of the advantages
  40. // of The Machinery is that specialized tools like this are relatively easy to write.
  41.  
  42. // Specifies the type of a log message.
  43. enum tm_log_type {
  44.     // Used for informational messages and command output.
  45.     TM_LOG_TYPE_INFO,
  46.  
  47.     // Used for debug prints when trying to diagnose a problem. Once the problem is fixed, all debug
  48.     // output should be removed.
  49.     TM_LOG_TYPE_DEBUG,
  50.  
  51.     // Used for error messages. This should only be used for actual errors and it should be possible
  52.     // for the user to fix the error and make the error message go away.
  53.     TM_LOG_TYPE_ERROR,
  54.  
  55.     // Used for extra logging while running in headless mode.
  56.     TM_LOG_TYPE_HEADLESS,
  57. };
  58.  
  59. typedef struct tm_logger_o tm_logger_o;
  60.  
  61. // Interface for loggers. A logger receives log messages and does something
  62. // with them -- prints to a console, dumps to a file, etc.
  63. typedef struct tm_logger_i
  64. {
  65.     struct tm_logger_o *inst;
  66.  
  67.     // Logs the `msg` of type `log_type` to this logger.
  68.     void (*log)(struct tm_logger_o *inst, enum tm_log_type log_type, const char *msg);
  69. } tm_logger_i;
  70.  
  71. // Manages a list of active loggers. You can register more loggers to add more backend outputs for
  72. // log messages.
  73. struct tm_logger_api
  74. {
  75.     // Adds a new logger to the registry.
  76.     void (*add_logger)(const tm_logger_i *logger);
  77.  
  78.     // Removes a previously added logger from the registry.
  79.     void (*remove_logger)(const tm_logger_i *logger);
  80.  
  81.     // Sends a log message to all registered loggers.
  82.     void (*print)(enum tm_log_type log_type, const char *msg);
  83.  
  84.     // Convenience function for sending a formatted string message to all registered loggers.
  85.     int (*printf)(enum tm_log_type log_type, const char *format, ...);
  86.  
  87.     // A default logger that will print log messages using `printf(...)`.
  88.     //
  89.     // On Windows, these messages are also printed using `OutputDebugString()` so they appear in
  90.     // Visual Studios log console.
  91.     //
  92.     // Note that this logger is automatically added. You need to explicitly remove it, if you don't want to use it.
  93.     tm_logger_i *default_logger;
  94. };
  95.  
  96. #define tm_logger_api_version TM_VERSION(1, 0, 0)
  97.  
  98. // Convenience macro for quick logging. Messages logged using this macro use the
  99. // [[TM_LOG_TYPE_INFO]] type.
  100. #define TM_LOG(format, ...) tm_logger_api->printf(TM_LOG_TYPE_INFO, "" format "", ##__VA_ARGS__)
  101.  
  102. // Convenience macro for quick logging. Messages logged using this macro use the
  103. // [[TM_LOG_TYPE_HEADLESS]] type. They will not be logged unless [[TM_FEATURE_FLAG__HEADLESS_LOGGING]]
  104. // is true
  105. #define TM_LOG_HEADLESS(format, ...) tm_logger_api->printf(TM_LOG_TYPE_HEADLESS, "" format "", ##__VA_ARGS__)
  106.  
  107. #if defined(TM_LINKS_FOUNDATION)
  108. extern struct tm_logger_api *tm_logger_api;
  109. #endif
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement