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.

118 lines
3.2KB

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