tor_log.rs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. // See LICENSE for licensing information */
  3. // Note that these functions are untested due to the fact that there are no
  4. // return variables to test and they are calling into a C API.
  5. /// The related domain which the logging message is relevant. For example,
  6. /// log messages relevant to networking would use LogDomain::LdNet, whereas
  7. /// general messages can use LdGeneral.
  8. #[derive(Eq, PartialEq)]
  9. pub enum LogDomain {
  10. LdNet,
  11. LdGeneral,
  12. }
  13. /// The severity level at which to log messages.
  14. #[derive(Eq, PartialEq)]
  15. pub enum LogSeverity {
  16. Notice,
  17. Warn,
  18. }
  19. /// Main entry point for Rust modules to log messages.
  20. ///
  21. /// # Inputs
  22. ///
  23. /// * A `severity` of type LogSeverity, which defines the level of severity the
  24. /// message will be logged.
  25. /// * A `domain` of type LogDomain, which defines the domain the log message
  26. /// will be associated with.
  27. /// * A `function` of type &str, which defines the name of the function where
  28. /// the message is being logged. There is a current RFC for a macro that
  29. /// defines function names. When it is, we should use it. See
  30. /// https://github.com/rust-lang/rfcs/pull/1719
  31. /// * A `message` of type &str, which is the log message itself.
  32. #[macro_export]
  33. macro_rules! tor_log_msg {
  34. ($severity: path,
  35. $domain: path,
  36. $function: expr,
  37. $($message:tt)*) =>
  38. {
  39. {
  40. let msg = format!($($message)*);
  41. $crate::tor_log_msg_impl($severity, $domain, $function, msg)
  42. }
  43. };
  44. }
  45. #[inline]
  46. pub fn tor_log_msg_impl(
  47. severity: LogSeverity,
  48. domain: LogDomain,
  49. function: &str,
  50. message: String,
  51. ) {
  52. use std::ffi::CString;
  53. /// Default function name to log in case of errors when converting
  54. /// a function name to a CString
  55. const ERR_LOG_FUNCTION: &str = "tor_log_msg";
  56. /// Default message to log in case of errors when converting a log
  57. /// message to a CString
  58. const ERR_LOG_MSG: &str = "Unable to log message from Rust
  59. module due to error when converting to CString";
  60. let func = match CString::new(function) {
  61. Ok(n) => n,
  62. Err(_) => CString::new(ERR_LOG_FUNCTION).unwrap(),
  63. };
  64. let msg = match CString::new(message) {
  65. Ok(n) => n,
  66. Err(_) => CString::new(ERR_LOG_MSG).unwrap(),
  67. };
  68. // Bind to a local variable to preserve ownership. This is essential so
  69. // that ownership is guaranteed until these local variables go out of scope
  70. let func_ptr = func.as_ptr();
  71. let msg_ptr = msg.as_ptr();
  72. let c_severity = unsafe { log::translate_severity(severity) };
  73. let c_domain = unsafe { log::translate_domain(domain) };
  74. unsafe { log::tor_log_string(c_severity, c_domain, func_ptr, msg_ptr) }
  75. }
  76. /// This module exposes no-op functionality for testing other Rust modules
  77. /// without linking to C.
  78. #[cfg(any(test, feature = "testing"))]
  79. pub mod log {
  80. use libc::{c_char, c_int};
  81. use super::LogDomain;
  82. use super::LogSeverity;
  83. pub unsafe fn tor_log_string<'a>(
  84. _severity: c_int,
  85. _domain: u32,
  86. _function: *const c_char,
  87. _message: *const c_char,
  88. ) {
  89. }
  90. pub unsafe fn translate_domain(_domain: LogDomain) -> u32 {
  91. 1
  92. }
  93. pub unsafe fn translate_severity(_severity: LogSeverity) -> c_int {
  94. 1
  95. }
  96. }
  97. /// This implementation is used when compiling for actual use, as opposed to
  98. /// testing.
  99. #[cfg(all(not(test), not(feature = "testing")))]
  100. pub mod log {
  101. use libc::{c_char, c_int};
  102. use super::LogDomain;
  103. use super::LogSeverity;
  104. /// Severity log types. These mirror definitions in /src/common/torlog.h
  105. /// C_RUST_COUPLED: src/common/log.c, log domain types
  106. extern "C" {
  107. #[no_mangle]
  108. static _LOG_WARN: c_int;
  109. static _LOG_NOTICE: c_int;
  110. }
  111. /// Domain log types. These mirror definitions in /src/common/torlog.h
  112. /// C_RUST_COUPLED: src/common/log.c, log severity types
  113. extern "C" {
  114. #[no_mangle]
  115. static _LD_NET: u32;
  116. static _LD_GENERAL: u32;
  117. }
  118. /// Translate Rust defintions of log domain levels to C. This exposes a 1:1
  119. /// mapping between types.
  120. pub unsafe fn translate_domain(domain: LogDomain) -> u32 {
  121. match domain {
  122. LogDomain::LdNet => _LD_NET,
  123. LogDomain::LdGeneral => _LD_GENERAL,
  124. }
  125. }
  126. /// Translate Rust defintions of log severity levels to C. This exposes a
  127. /// 1:1 mapping between types.
  128. pub unsafe fn translate_severity(severity: LogSeverity) -> c_int {
  129. match severity {
  130. LogSeverity::Warn => _LOG_WARN,
  131. LogSeverity::Notice => _LOG_NOTICE,
  132. }
  133. }
  134. /// The main entry point into Tor's logger. When in non-test mode, this
  135. /// will link directly with `tor_log_string` in /src/or/log.c
  136. extern "C" {
  137. pub fn tor_log_string(
  138. severity: c_int,
  139. domain: u32,
  140. function: *const c_char,
  141. string: *const c_char,
  142. );
  143. }
  144. }