s2time.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2009 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. #include "config.h"
  25. #include "s2util.h"
  26. #include <string.h>
  27. #include <stdio.h>
  28. /* -- Delay scenarios --------------------------------------------------- */
  29. static unsigned long time_offset;
  30. extern void (*_su_time)(su_time_t *tv);
  31. static void _su_time_fast_forwarder(su_time_t *tv)
  32. {
  33. tv->tv_sec += time_offset;
  34. }
  35. void s2_fast_forward(unsigned long seconds,
  36. su_root_t *root)
  37. {
  38. if (_su_time == NULL)
  39. _su_time = _su_time_fast_forwarder;
  40. time_offset += seconds;
  41. if (root)
  42. su_root_step(root, 0);
  43. }
  44. void
  45. s2_timed_logger(void *stream, char const *fmt, va_list ap)
  46. {
  47. char buffer[4096];
  48. su_time_t now = su_now();
  49. size_t prefix, wrote;
  50. int n;
  51. snprintf(buffer, sizeof buffer,
  52. "%02u:%02u:%02u.%06lu[+%lu] ",
  53. (unsigned)(now.tv_sec / 3600 % 24),
  54. (unsigned)(now.tv_sec / 60 % 60),
  55. (unsigned)(now.tv_sec % 60),
  56. now.tv_usec,
  57. time_offset);
  58. prefix = strlen(buffer);
  59. n = vsnprintf(buffer + prefix, (sizeof buffer) - prefix, fmt, ap);
  60. if (n > 0)
  61. wrote = fwrite(buffer, prefix + n, 1, stream);
  62. }