haicrypt_log.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * SRT - Secure, Reliable, Transport
  3. * Copyright (c) 2018 Haivision Systems Inc.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. *
  9. */
  10. #if ENABLE_HAICRYPT_LOGGING
  11. #include "haicrypt_log.h"
  12. #include "hcrypt.h"
  13. #include "haicrypt.h"
  14. #include "../srtcore/srt.h"
  15. #include "../srtcore/logging.h"
  16. extern srt_logging::LogConfig srt_logger_config;
  17. // LOGFA symbol defined in srt.h
  18. srt_logging::Logger hclog(SRT_LOGFA_HAICRYPT, srt_logger_config, "SRT.hc");
  19. extern "C" {
  20. int HaiCrypt_SetLogLevel(int level, int logfa)
  21. {
  22. srt_setloglevel(level);
  23. if (logfa != SRT_LOGFA_GENERAL) // General can't be turned on or off
  24. {
  25. srt_addlogfa(logfa);
  26. }
  27. return 0;
  28. }
  29. // HaiCrypt will be using its own FA, which will be turned off by default.
  30. // Templates made C way.
  31. // It's tempting to use the HAICRYPT_DEFINE_LOG_DISPATCHER macro here because it would provide the
  32. // exact signature that is needed here, the problem is though that this would expand the LOGLEVEL
  33. // parameter, which is also a macro, into the value that the macro designates, which would generate
  34. // the HaiCrypt_LogF_0 instead of HaiCrypt_LogF_LOG_DEBUG, for example.
  35. #define HAICRYPT_DEFINE_LOG_DISPATCHER(LOGLEVEL, dispatcher) \
  36. int HaiCrypt_LogF_##LOGLEVEL ( const char* file, int line, const char* function, const char* format, ...) \
  37. { \
  38. srt_logging::LogDispatcher& lg = hclog.dispatcher; \
  39. if (!lg.CheckEnabled()) return -1; \
  40. va_list ap; \
  41. va_start(ap, format); \
  42. lg().setloc(file, line, function).vform(format, ap); \
  43. va_end(ap); \
  44. return 0; \
  45. }
  46. HAICRYPT_DEFINE_LOG_DISPATCHER(LOG_DEBUG, Debug);
  47. HAICRYPT_DEFINE_LOG_DISPATCHER(LOG_NOTICE, Note);
  48. HAICRYPT_DEFINE_LOG_DISPATCHER(LOG_INFO, Note);
  49. HAICRYPT_DEFINE_LOG_DISPATCHER(LOG_WARNING, Warn);
  50. HAICRYPT_DEFINE_LOG_DISPATCHER(LOG_ERR, Error);
  51. HAICRYPT_DEFINE_LOG_DISPATCHER(LOG_CRIT, Fatal);
  52. HAICRYPT_DEFINE_LOG_DISPATCHER(LOG_ALERT, Fatal);
  53. HAICRYPT_DEFINE_LOG_DISPATCHER(LOG_EMERG, Fatal);
  54. static void DumpCfgFlags(int flags, std::ostream& out)
  55. {
  56. static struct { int flg; const char* desc; } flgtable [] = {
  57. #define HCRYPTF(name) { HAICRYPT_CFG_F_##name, #name }
  58. HCRYPTF(TX),
  59. HCRYPTF(CRYPTO),
  60. HCRYPTF(FEC)
  61. #undef HCRYPTF
  62. };
  63. size_t flgtable_size = sizeof(flgtable)/sizeof(flgtable[0]);
  64. size_t i;
  65. out << "{";
  66. const char* sep = "";
  67. const char* sep_bar = " | ";
  68. for (i = 0; i < flgtable_size; ++i)
  69. {
  70. if ( (flgtable[i].flg & flags) != 0 )
  71. {
  72. out << sep << flgtable[i].desc;
  73. sep = sep_bar;
  74. }
  75. }
  76. out << "}";
  77. }
  78. void HaiCrypt_DumpConfig(const HaiCrypt_Cfg* cfg)
  79. {
  80. std::ostringstream cfg_flags;
  81. DumpCfgFlags(cfg->flags, cfg_flags);
  82. LOGC(hclog.Debug, log << "CFG DUMP: flags=" << cfg_flags.str()
  83. << " xport=" << (cfg->xport == HAICRYPT_XPT_SRT ? "SRT" : "INVALID")
  84. << " cipher="
  85. << CRYSPR_IMPL_DESC
  86. << " key_len=" << cfg->key_len << " data_max_len=" << cfg->data_max_len);
  87. LOGC(hclog.Debug, log << "CFG DUMP: txperiod="
  88. << cfg->km_tx_period_ms << "ms kmrefresh=" << cfg->km_refresh_rate_pkt
  89. << " kmpreannounce=" << cfg->km_pre_announce_pkt
  90. << " secret "
  91. << "{tp=" << (cfg->secret.typ == 1 ? "PSK" : cfg->secret.typ == 2 ? "PWD" : "???")
  92. << " len=" << cfg->secret.len << " pwd=" << cfg->secret.str << "}");
  93. }
  94. } // extern "C"
  95. #endif // Block for the whole file