Skip to content
Browse files

Added spiffy-split-log command line tool

  • Loading branch information...
1 parent c2845b1 commit d8d690f9a2001cf10c89393beb720ee111d94a3b @mario-goulart committed Feb 5, 2012
Showing with 94 additions and 17 deletions.
  1. +15 −0 common.scm
  2. +2 −14 spiffy-sexpr-log.scm
  3. +10 −3 spiffy-sexpr-log.setup
  4. +67 −0 spiffy-split-log.scm
View
15 common.scm
@@ -0,0 +1,15 @@
+(use srfi-13)
+
+(define (pad-number n zeroes)
+ (define (pad num len)
+ (let ((str (if (string? num) num (number->string num))))
+ (if (equal? str "")
+ ""
+ (if (>= (string-length str) len)
+ str
+ (string-pad str len #\0)))))
+
+ (let ((len (string-length (->string n))))
+ (if (= len zeroes)
+ n
+ (pad n zeroes))))
View
16 spiffy-sexpr-log.scm
@@ -3,21 +3,9 @@
(import chicken scheme posix data-structures srfi-13 files)
(use spiffy intarweb uri-common)
-(define split-log? (make-parameter #f))
-
-(define (pad-number n zeroes)
- (define (pad num len)
- (let ((str (if (string? num) num (number->string num))))
- (if (equal? str "")
- ""
- (if (>= (string-length str) len)
- str
- (string-pad str len #\0)))))
+(include "common.scm")
- (let ((len (string-length (->string n))))
- (if (= len zeroes)
- n
- (pad n zeroes))))
+(define split-log? (make-parameter #f))
(define (get-current-log-file)
View
13 spiffy-sexpr-log.setup
@@ -4,13 +4,20 @@
(compile -S -s -O3 -d1 -J spiffy-sexpr-log.scm)
(compile -S -s -O3 -d1 spiffy-sexpr-log.import.scm)
-;; Compile the application
+;; Compile the applications
(compile -S -O3 -d1 spiffy-log2sexpr.scm -o spiffy-log2sexpr)
+(compile -S -O3 -d1 spiffy-split-log.scm -o spiffy-split-log)
+
+(define spiffy-sexpr-log-version "0.3")
(install-extension 'spiffy-sexpr-log
'("spiffy-sexpr-log.so" "spiffy-sexpr-log.import.so")
- '((version "0.2")))
+ `((version ,spiffy-sexpr-log-version)))
(install-program 'spiffy-log2sexpr
'("spiffy-log2sexpr")
- `((version "0.3")))
+ `((version spiffy-sexpr-log-version)))
+
+(install-program 'spiffy-split-log
+ '("spiffy-split-log")
+ `((version spiffy-sexpr-log-version)))
View
67 spiffy-split-log.scm
@@ -0,0 +1,67 @@
+(use posix srfi-1 utils)
+
+(include "common.scm")
+
+(define-record date year month day)
+
+(define months
+ '((Jan . "01")
+ (Feb . "02")
+ (Mar . "03")
+ (Apr . "04")
+ (May . "05")
+ (Jun . "06")
+ (Jul . "07")
+ (Aug . "08")
+ (Sep . "09")
+ (Oct . "10")
+ (Nov . "11")
+ (Dec . "12")))
+
+(define (parse-date date)
+ (let ((tokens (string-split date)))
+ (make-date (last tokens)
+ (alist-ref (string->symbol (cadr tokens)) months)
+ (caddr tokens))))
+
+
+(define (log-line line log-file)
+ (with-output-to-file log-file
+ (lambda ()
+ (printf "~S\n" line))
+ append:))
+
+
+(define (split-log log-file dont-ask?)
+ (let ((data (read-file log-file))
+ (overwritten-log-files '()))
+ (for-each (lambda (line)
+ (let* ((d (parse-date (cadr line)))
+ (dir (make-pathname (date-year d) (date-month d)))
+ (split-log-file
+ (make-pathname dir (pad-number (date-day d) 2) "log")))
+ (unless (directory-exists? dir)
+ (create-directory dir 'with-parents))
+ ;;; FIXME: handle --dont-ask
+ (log-line line split-log-file)))
+ data)))
+
+(define (usage #!optional exit-code)
+ (print "Usage: " (pathname-strip-directory (program-name)) " [ --dont-ask ] <log file>")
+ (print #<<EOF
+
+Note: When called with --dont-ask, this program reuses log
+directories (e.g., if file 2012/01/27.log exists, it appends data to
+its end). Thus, if this program is executed twice without having the
+output log file deleted after the first run, the log entries will be
+duplicated.
+EOF
+)
+ (when exit-code (exit exit-code)))
+
+
+(let* ((args (command-line-arguments))
+ (dont-ask? (and (member "--dont-ask" args) #t)))
+ (when (null? args)
+ (usage 1))
+ (split-log (last args) dont-ask?))

0 comments on commit d8d690f

Please sign in to comment.
Something went wrong with that request. Please try again.