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.

109 lines
2.9KB

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