IndirectLog.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "util/log/Log_impl.h"
  16. #include "util/log/IndirectLog.h"
  17. #include "util/Identity.h"
  18. #include <stdint.h>
  19. struct IndirectLog_pvt
  20. {
  21. struct Log log;
  22. struct Log* wrapped;
  23. Identity
  24. };
  25. static void doLog(struct Log* genericLog,
  26. enum Log_Level logLevel,
  27. const char* file,
  28. int lineNum,
  29. const char* format,
  30. va_list args)
  31. {
  32. struct IndirectLog_pvt* il = Identity_check((struct IndirectLog_pvt*) genericLog);
  33. if (il && il->wrapped) {
  34. il->wrapped->print(il->wrapped, logLevel, file, lineNum, format, args);
  35. }
  36. }
  37. struct Log* IndirectLog_new(struct Allocator* alloc)
  38. {
  39. struct IndirectLog_pvt* il = Allocator_clone(alloc, (&(struct IndirectLog_pvt) {
  40. .log = {
  41. .print = doLog
  42. }
  43. }));
  44. Identity_set(il);
  45. return &il->log;
  46. }
  47. void IndirectLog_set(struct Log* indirectLog, struct Log* wrapped)
  48. {
  49. struct IndirectLog_pvt* il = Identity_check((struct IndirectLog_pvt*) indirectLog);
  50. il->wrapped = wrapped;
  51. }