You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.4KB

  1. //! Tools to record and display what's happening in your program
  2. //!
  3. #![feature(test)]
  4. #[macro_use]
  5. extern crate slog;
  6. #[allow(unused_imports)]
  7. #[macro_use]
  8. extern crate money;
  9. #[cfg(test)]
  10. extern crate test;
  11. #[cfg(feature = "zmq")]
  12. extern crate zmq;
  13. #[cfg(feature = "latency")]
  14. extern crate pubsub as pub_sub;
  15. use std::time::Duration;
  16. use chrono::{DateTime, Utc, TimeZone as ChronoTZ};
  17. #[allow(unused_imports)]
  18. use sloggers::Build;
  19. #[allow(unused_imports)]
  20. pub use sloggers::types::Severity;
  21. use sloggers::types::TimeZone;
  22. #[allow(unused_imports)]
  23. use sloggers::file::FileLoggerBuilder;
  24. use slog::Drain;
  25. use uuid::Uuid;
  26. pub mod influx;
  27. #[cfg(feature = "warnings")]
  28. pub mod warnings;
  29. #[cfg(feature = "latency")]
  30. pub mod latency;
  31. pub mod hist;
  32. #[cfg(feature = "trace")]
  33. pub const LOG_LEVEL: Severity = Severity::Trace;
  34. #[cfg(all(feature = "debug", not(feature = "trace")))]
  35. pub const LOG_LEVEL: Severity = Severity::Debug;
  36. #[cfg(not(any(feature = "debug", feature = "trace")))]
  37. pub const LOG_LEVEL: Severity = Severity::Info;
  38. #[cfg(not(feature = "trace"))]
  39. const CHANNEL_SIZE: usize = 32_768;
  40. #[cfg(feature = "trace")]
  41. const CHANNEL_SIZE: usize = 2_097_152;
  42. /// converts a chrono::DateTime to an integer timestamp (ns)
  43. ///
  44. #[inline]
  45. pub fn nanos(t: DateTime<Utc>) -> u64 {
  46. (t.timestamp() as u64) * 1_000_000_000_u64 + (t.timestamp_subsec_nanos() as u64)
  47. }
  48. #[inline]
  49. pub fn secs(d: Duration) -> f64 {
  50. d.as_secs() as f64 + d.subsec_nanos() as f64 / 1_000_000_000_f64
  51. }
  52. #[inline]
  53. pub fn inanos(t: DateTime<Utc>) -> i64 {
  54. t.timestamp() * 1_000_000_000i64 + t.timestamp_subsec_nanos() as i64
  55. }
  56. //#[cfg(not(any(test, feature = "test")))]
  57. pub fn file_logger<P: AsRef<std::path::Path>>(path: P, level: Severity) -> slog::Logger {
  58. let mut builder = FileLoggerBuilder::new(path);
  59. builder.level(level)
  60. .timezone(TimeZone::Utc)
  61. .channel_size(CHANNEL_SIZE)
  62. .rotate_size(1024 * 1024 * 1024)
  63. .rotate_keep(1000)
  64. .rotate_compress(true)
  65. .source_location(sloggers::types::SourceLocation::ModuleAndLine);
  66. builder.build().unwrap() // the sloggers impl can't actually fail (v0.3)
  67. }
  68. pub fn truncating_file_logger(path: &str, level: Severity) -> slog::Logger {
  69. let mut builder = FileLoggerBuilder::new(path);
  70. builder.level(level);
  71. builder.timezone(TimeZone::Utc);
  72. builder.truncate();
  73. builder.channel_size(CHANNEL_SIZE);
  74. builder.build().unwrap()
  75. }
  76. #[deprecated(since="0.4.0", note="Turns out the file logger in sloggers uses async, \
  77. making the async here duplicative")]
  78. pub fn async_file_logger(path: &str, level: Severity) -> slog::Logger {
  79. let drain = file_logger(path, level);
  80. let async_drain =
  81. slog_async::Async::new(drain)
  82. .chan_size(100_000)
  83. .build();
  84. slog::Logger::root(async_drain.fuse(), o!())
  85. }
  86. //#[deprecated(since="0.4.3", note="Use `nanos(DateTime<Utc>) -> u64` instead")]
  87. pub fn dt_nanos(t: DateTime<Utc>) -> i64 {
  88. (t.timestamp() as i64) * 1_000_000_000_i64 + (t.timestamp_subsec_nanos() as i64)
  89. }
  90. #[inline]
  91. pub fn dur_nanos(d: ::std::time::Duration) -> i64 {
  92. (d.as_secs() * 1_000_000_000_u64 + (d.subsec_nanos() as u64)) as i64
  93. }
  94. #[inline]
  95. pub fn nanos_utc(t: i64) -> DateTime<Utc> {
  96. Utc.timestamp(t / 1_000_000_000, (t % 1_000_000_000) as u32)
  97. }
  98. pub fn short_uuid(id: &Uuid) -> String {
  99. if cfg!(feature = "disable-short-uuid") {
  100. id.to_string()
  101. } else {
  102. format!("{}", &id.to_string()[..8])
  103. }
  104. }