Annotation of src/usr.bin/mg/log.c, Revision 1.1
1.1 ! lum 1: /* $OpenBSD$ */
! 2:
! 3: /*
! 4: * This file is in the public domain.
! 5: *
! 6: * Author: Mark Lumsden <mark@showcomplex.com>
! 7: *
! 8: */
! 9:
! 10: /*
! 11: * Record a history of an mg session for temporal debugging.
! 12: * Sometimes pressing a key will set the scene for a bug only visible
! 13: * dozens of keystrokes later. gdb has its limitations in this scenario.
! 14: */
! 15:
! 16: #include <sys/queue.h>
! 17: #include <sys/stat.h>
! 18: #include <ctype.h>
! 19: #include <fcntl.h>
! 20: #include <signal.h>
! 21: #include <stdio.h>
! 22: #include <stdlib.h>
! 23: #include <string.h>
! 24: #include <unistd.h>
! 25:
! 26: #include "def.h"
! 27: #include "log.h"
! 28: #include "funmap.h"
! 29:
! 30: char mglogpath[20];
! 31:
! 32: int
! 33: mglog(PF funct)
! 34: {
! 35: struct line *lp;
! 36: struct stat sb;
! 37: char *curline;
! 38: FILE *fd;
! 39: int i;
! 40:
! 41: i = 0;
! 42:
! 43: if(stat(mglogpath, &sb))
! 44: return (FALSE);
! 45:
! 46: fd = fopen(mglogpath, "a");
! 47: if (fprintf(fd, "%s\n", function_name(funct)) == -1) {
! 48: fclose(fd);
! 49: return (FALSE);
! 50: }
! 51: lp = bfirstlp(curbp);
! 52:
! 53: for(;;) {
! 54: i++;
! 55: curline = " ";
! 56: if (i == curwp->w_dotline)
! 57: curline = ">";
! 58: if (fprintf(fd, "%s%p b^%p f.%p %d %d\t|%s\n", curline,
! 59: lp, lp->l_bp, lp->l_fp,
! 60: lp->l_size, lp->l_used, lp->l_text) == -1) {
! 61: fclose(fd);
! 62: return (FALSE);
! 63: }
! 64: lp = lforw(lp);
! 65: if (lp == curbp->b_headp) {
! 66: if (fprintf(fd, " %p b^%p f.%p [bhead]\n(EOB)\n",
! 67: lp, lp->l_bp, lp->l_fp) == -1) {
! 68: fclose(fd);
! 69: return (FALSE);
! 70: }
! 71: if (fprintf(fd, "lines:raw%d buf%d wdot%d\n\n",
! 72: i, curbp->b_lines, curwp->w_dotline)) {
! 73: fclose(fd);
! 74: return (FALSE);
! 75: }
! 76: break;
! 77: }
! 78: }
! 79: fclose(fd);
! 80:
! 81: return (TRUE);
! 82: }
! 83:
! 84:
! 85: /*
! 86: * Make sure logging to log file can happen.
! 87: */
! 88: int
! 89: mgloginit(void)
! 90: {
! 91: struct stat sb;
! 92: mode_t dir_mode, f_mode, oumask;
! 93: char *mglogdir, *mglogfile;
! 94: int fd;
! 95:
! 96: mglogdir = "./log/";
! 97: mglogfile = "mg.log";
! 98:
! 99: oumask = umask(0);
! 100: f_mode = 0777& ~oumask;
! 101: dir_mode = f_mode | S_IWUSR | S_IXUSR;
! 102:
! 103: if(stat(mglogdir, &sb)) {
! 104: if (mkdir(mglogdir, dir_mode) != 0)
! 105: return (FALSE);
! 106: if (chmod(mglogdir, f_mode) < 0)
! 107: return (FALSE);
! 108: }
! 109: if (strlcpy(mglogpath, mglogdir, sizeof(mglogpath)) >
! 110: sizeof(mglogpath))
! 111: return (FALSE);
! 112: if (strlcat(mglogpath, mglogfile, sizeof(mglogpath)) >
! 113: sizeof(mglogpath))
! 114: return (FALSE);
! 115:
! 116: if(stat(mglogpath, &sb))
! 117: fd = open(mglogpath, O_RDWR | O_CREAT | O_TRUNC, 0644);
! 118: else
! 119: fd = open(mglogpath, O_RDWR | O_TRUNC, 0644);
! 120:
! 121: if (fd == -1)
! 122: return (FALSE);
! 123:
! 124: close(fd);
! 125:
! 126: return (TRUE);
! 127: }