log_file.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * File : log_file.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2013, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * Bernard the first version
  23. * 2013-06-26 Grissiom refactor
  24. */
  25. #ifdef RT_USING_DFS
  26. #include <rtthread.h>
  27. #include <log_trace.h>
  28. #include <dfs_posix.h>
  29. struct file_device
  30. {
  31. struct rt_device parent;
  32. int fd;
  33. char *filename;
  34. };
  35. /* file device for log trace */
  36. static struct file_device _file_device;
  37. /* common device interface */
  38. rt_err_t fdevice_open(rt_device_t dev, rt_uint16_t oflag)
  39. {
  40. int fd;
  41. struct file_device *file = (struct file_device *)dev;
  42. if (file->fd >= 0) return -RT_EBUSY;
  43. fd = open(file->filename, O_RDONLY, 0);
  44. if (fd >= 0)
  45. {
  46. close(fd);
  47. /* file exists */
  48. fd = open(file->filename, O_WRONLY | O_APPEND, 0);
  49. }
  50. else
  51. {
  52. /* file not exists */
  53. fd = open(file->filename, O_WRONLY | O_CREAT, 0);
  54. }
  55. file->fd = fd;
  56. return RT_EOK;
  57. }
  58. rt_err_t fdevice_close(rt_device_t dev)
  59. {
  60. rt_err_t result;
  61. struct file_device *file = (struct file_device *)dev;
  62. if (file->fd < 0) return -RT_EBUSY;
  63. result = close(file->fd);
  64. if (result == 0)
  65. {
  66. file->fd = -1;
  67. }
  68. return result;
  69. }
  70. rt_size_t fdevice_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  71. {
  72. struct file_device *file = (struct file_device *)dev;
  73. if (file->fd < 0) return 0;
  74. return write(file->fd, buffer, size);
  75. }
  76. void log_trace_file_init(const char *filename)
  77. {
  78. rt_device_t device;
  79. device = rt_device_find("logfile");
  80. if (device == RT_NULL)
  81. {
  82. rt_memset(&_file_device, 0x00, sizeof(_file_device));
  83. _file_device.parent.type = RT_Device_Class_Char;
  84. _file_device.parent.init = RT_NULL;
  85. _file_device.parent.open = fdevice_open;
  86. _file_device.parent.close = fdevice_close;
  87. _file_device.parent.write = fdevice_write;
  88. rt_device_register(&_file_device.parent, "logfile", O_RDWR);
  89. }
  90. _file_device.filename = rt_strdup(filename);
  91. _file_device.fd = -1;
  92. }
  93. #endif // RT_USING_DFS