debug.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * \file debug.c
  3. *
  4. * \brief GIS Library - Debug functions.
  5. *
  6. * (C) 2001-2008 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author GRASS GIS Development Team
  12. *
  13. * \date 1999-2008
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <stdarg.h>
  19. #include <grass/gis.h>
  20. #include <grass/glocale.h>
  21. #include "G.h"
  22. static int initialized;
  23. static int grass_debug_level;
  24. /**
  25. * \brief Print debugging message.
  26. *
  27. * Print debugging message if environment variable GRASS_DEBUG_LEVEL
  28. * is set to level equal or greater
  29. *
  30. * Levels: (recommended levels)<br>
  31. * - 1 - message is printed once or few times per module<br>
  32. * - 3 - each row (raster) or line (vector)<br>
  33. * - 5 - each cell (raster) or point (vector)
  34. *
  35. * \param[in] level level
  36. * \param[in] msg message
  37. * \return 0 on error
  38. * \return 1 on success
  39. */
  40. void G_init_debug(void)
  41. {
  42. const char *lstr;
  43. if (G_is_initialized(&initialized))
  44. return;
  45. lstr = G__getenv("DEBUG");
  46. if (lstr != NULL)
  47. grass_debug_level = atoi(lstr);
  48. else
  49. grass_debug_level = 0;
  50. G_initialize_done(&initialized);
  51. }
  52. int G_debug(int level, const char *msg, ...)
  53. {
  54. char *filen;
  55. va_list ap;
  56. FILE *fd;
  57. G_init_debug();
  58. if (grass_debug_level >= level) {
  59. va_start(ap, msg);
  60. filen = getenv("GRASS_DEBUG_FILE");
  61. if (filen != NULL) {
  62. fd = fopen(filen, "a");
  63. if (!fd) {
  64. G_warning(_("Cannot open debug file '%s'"), filen);
  65. return 0;
  66. }
  67. }
  68. else {
  69. fd = stderr;
  70. }
  71. fprintf(fd, "D%d/%d: ", level, grass_debug_level);
  72. vfprintf(fd, msg, ap);
  73. fprintf(fd, "\n");
  74. fflush(fd);
  75. if (filen != NULL)
  76. fclose(fd);
  77. va_end(ap);
  78. }
  79. return 1;
  80. }
  81. /**
  82. * \brief Dumps status of various GIS parameters.
  83. *
  84. * Dumps status of various GIS parameters of a particular
  85. * file descriptor, <b>fd</b>.
  86. *
  87. * \param[in] fd file
  88. * \return always returns 0
  89. */
  90. int G_dump(int fd)
  91. {
  92. const struct fileinfo *fcb = &G__.fileinfo[fd];
  93. G_message("G_dump: memory allocated to G__");
  94. G_message("type for writing floating maps = %d", G__.fp_type);
  95. G_message("current window = %p", &G__.window);
  96. G_message("Flag: window set? %d", G__.window_set);
  97. G_message("File descriptor for automatic mask %d", G__.mask_fd);
  98. G_message("Flag denoting automatic masking %d", G__.auto_mask);
  99. G_message("Histogram request %d", G__.want_histogram);
  100. G_message("G_dump: file #%d", fd);
  101. G_message("open mode = %d", fcb->open_mode);
  102. G_message("Cell header %p", &fcb->cellhd);
  103. G_message("Table reclass %p", &fcb->reclass);
  104. G_message("Cell stats %p", &fcb->statf);
  105. G_message("Range structure %p", &fcb->range);
  106. G_message("float Range structure %p", &fcb->fp_range);
  107. G_message("want histogram? %d", fcb->want_histogram);
  108. G_message("Automatic reclass flag %d", fcb->reclass_flag);
  109. G_message("File row addresses %p", fcb->row_ptr);
  110. G_message("Data to window col mapping %p", fcb->col_map);
  111. G_message("Data to window row constants %f,%f", fcb->C1, fcb->C2);
  112. G_message("Current data row in memory %d", fcb->cur_row);
  113. G_message("Current null row in memory %d", fcb->null_cur_row);
  114. G_message("nbytes per cell for current row %d", fcb->cur_nbytes);
  115. G_message("Decompressed data buffer %s", fcb->data);
  116. G_message("bytes per cell %d", fcb->nbytes);
  117. G_message("type: int, float or double map %d", fcb->map_type);
  118. G_message("Temporary name for NEW files %s", fcb->temp_name);
  119. G_message("Temporary name for NEW NULL files %s", fcb->null_temp_name);
  120. G_message("for existing raster maps %d", fcb->null_file_exists);
  121. G_message("Name of open file %s", fcb->name);
  122. G_message("Mapset of open file %s", fcb->mapset);
  123. G_message("io error warning given %d", fcb->io_error);
  124. G_message("xdr stream for reading fp %p", &fcb->xdrstream);
  125. G_message("NULL_ROWS array[%d] = %p", NULL_ROWS_INMEM, fcb->NULL_ROWS);
  126. G_message("Minimum row null number in memory %d", fcb->min_null_row);
  127. G_message("Quant ptr = %p", &fcb->quant);
  128. G_message("G_dump: end");
  129. return 0;
  130. }