debug.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 twice per module<br>
  32. * - 2 - less interesting once-per-module messages,<br>
  33. * - 2 - library functions likely to be used once in a module<br>
  34. * - 3- library functions likely to be called a few times in a module (<=10),<br>
  35. * - 3 - each row (raster) or line (vector) or database/column (DB)<br>
  36. * - 4 - each column/cat (DB)<br>
  37. * - 5 - each cell (raster) or point (vector) or cat/attribute (DB)
  38. *
  39. * \param[in] level level
  40. * \param[in] msg message
  41. * \return 0 on error
  42. * \return 1 on success
  43. */
  44. void G_init_debug(void)
  45. {
  46. const char *lstr;
  47. if (G_is_initialized(&initialized))
  48. return;
  49. lstr = G__getenv("DEBUG");
  50. if (lstr != NULL)
  51. grass_debug_level = atoi(lstr);
  52. else
  53. grass_debug_level = 0;
  54. G_initialize_done(&initialized);
  55. }
  56. int G_debug(int level, const char *msg, ...)
  57. {
  58. char *filen;
  59. va_list ap;
  60. FILE *fd;
  61. G_init_debug();
  62. if (grass_debug_level >= level) {
  63. va_start(ap, msg);
  64. filen = getenv("GRASS_DEBUG_FILE");
  65. if (filen != NULL) {
  66. fd = fopen(filen, "a");
  67. if (!fd) {
  68. G_warning(_("Cannot open debug file '%s'"), filen);
  69. return 0;
  70. }
  71. }
  72. else {
  73. fd = stderr;
  74. }
  75. fprintf(fd, "D%d/%d: ", level, grass_debug_level);
  76. vfprintf(fd, msg, ap);
  77. fprintf(fd, "\n");
  78. fflush(fd);
  79. if (filen != NULL)
  80. fclose(fd);
  81. va_end(ap);
  82. }
  83. return 1;
  84. }
  85. /**
  86. * \brief Dumps status of various GIS parameters.
  87. *
  88. * Dumps status of various GIS parameters of a particular
  89. * file descriptor, <b>fd</b>.
  90. *
  91. * \param[in] fd file
  92. * \return always returns 0
  93. */
  94. int G_dump(int fd)
  95. {
  96. const struct fileinfo *fcb = &G__.fileinfo[fd];
  97. G_message("G_dump: memory allocated to G__");
  98. G_message("type for writing floating maps = %d", G__.fp_type);
  99. G_message("current window = %p", &G__.window);
  100. G_message("Flag: window set? %d", G__.window_set);
  101. G_message("File descriptor for automatic mask %d", G__.mask_fd);
  102. G_message("Flag denoting automatic masking %d", G__.auto_mask);
  103. G_message("Histogram request %d", G__.want_histogram);
  104. G_message("G_dump: file #%d", fd);
  105. G_message("open mode = %d", fcb->open_mode);
  106. G_message("Cell header %p", &fcb->cellhd);
  107. G_message("Table reclass %p", &fcb->reclass);
  108. G_message("Cell stats %p", &fcb->statf);
  109. G_message("Range structure %p", &fcb->range);
  110. G_message("float Range structure %p", &fcb->fp_range);
  111. G_message("want histogram? %d", fcb->want_histogram);
  112. G_message("Automatic reclass flag %d", fcb->reclass_flag);
  113. G_message("File row addresses %p", fcb->row_ptr);
  114. G_message("Data to window col mapping %p", fcb->col_map);
  115. G_message("Data to window row constants %f,%f", fcb->C1, fcb->C2);
  116. G_message("Current data row in memory %d", fcb->cur_row);
  117. G_message("Current null row in memory %d", fcb->null_cur_row);
  118. G_message("nbytes per cell for current row %d", fcb->cur_nbytes);
  119. G_message("Decompressed data buffer %s", fcb->data);
  120. G_message("bytes per cell %d", fcb->nbytes);
  121. G_message("type: int, float or double map %d", fcb->map_type);
  122. G_message("Temporary name for NEW files %s", fcb->temp_name);
  123. G_message("Temporary name for NEW NULL files %s", fcb->null_temp_name);
  124. G_message("for existing raster maps %d", fcb->null_file_exists);
  125. G_message("Name of open file %s", fcb->name);
  126. G_message("Mapset of open file %s", fcb->mapset);
  127. G_message("io error warning given %d", fcb->io_error);
  128. G_message("xdr stream for reading fp %p", &fcb->xdrstream);
  129. G_message("NULL_ROWS array[%d] = %p", NULL_ROWS_INMEM, fcb->NULL_ROWS);
  130. G_message("Minimum row null number in memory %d", fcb->min_null_row);
  131. G_message("Quant ptr = %p", &fcb->quant);
  132. G_message("G_dump: end");
  133. return 0;
  134. }