Skip to content

Commit

Permalink
Adding logging facility.
Browse files Browse the repository at this point in the history
  • Loading branch information
nbeloglazov committed Jul 15, 2010
1 parent 95c58c5 commit 7c47549
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
# Ignore log file
log.txt
7 changes: 6 additions & 1 deletion README
Expand Up @@ -42,11 +42,16 @@ It must return map with following keys: :name :level-creator
:apple-generator - function, which takes level and returns new apple.
For example see basic.clj level.

## Logging

I decided to use logging in project. Now it logs all level changes to file log.txt.
You can change file in menu_gui.clj in *log-file* variable.
I use clojure.contrib.logging and log4j libraries.

## TODO
1. Extract from core.clj all functions, which are responsible for modifying
mutable data (like update-smth). But I don't know now what name new clj file and namespace should have.
2. Improve gui part of the game.
3. Add logging.
3. Create more levels.


Expand Down
Binary file modified lib/clojure-1.2.0-master-SNAPSHOT.jar
Binary file not shown.
Binary file modified lib/clojure-contrib-1.2.0-SNAPSHOT.jar
Binary file not shown.
Binary file added lib/log4j-1.2.16.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion project.clj
@@ -1,4 +1,5 @@
(defproject snakejure "0.1"
:description "Snake game written in clojure"
:dependencies [[org.clojure/clojure "1.2.0-master-SNAPSHOT"]
[org.clojure/clojure-contrib "1.2.0-SNAPSHOT"]])
[org.clojure/clojure-contrib "1.2.0-SNAPSHOT"]
[log4j/log4j "1.2.16"]])
13 changes: 13 additions & 0 deletions src/snakejure/core.clj
Expand Up @@ -26,6 +26,19 @@
[key `(~(keyword key) ~mp)])))
~@body))

(defn level-to-str
"Converts level to handy string for logging purpose.
If you specify one or more parts of level (e.g. :walls, :snake)
they will be excluded from string"
[level & excl]
(str "############################\n"
(->> level
(keys)
(remove #(includes? excl %))
(map #(str % " " (print-str (level %)) \newline))
(apply str))
"############################"))

(defn create-wall
"Creates new wall in given point."
[p]
Expand Down
5 changes: 4 additions & 1 deletion src/snakejure/game_gui.clj
@@ -1,5 +1,6 @@
(ns snakejure.game-gui
(:use (clojure.contrib import-static)
(:use (clojure.contrib import-static
logging)
(snakejure core))
(:require (snakejure.levels basic))
(:import (java.awt Dimension)
Expand Down Expand Up @@ -73,6 +74,7 @@
(actionPerformed [e]
(let-map [@level snake noisers walls]
(update-snake-position level)
(trace (level-to-str @level :walls))
(when (lose? snake walls)
(end-fn :lose)
(switch-timer (.getSource e)))
Expand All @@ -97,6 +99,7 @@
(defn create-game-panel [lvl end-fn]
"Creates panel, which displays game level.
end-fn will be called when user wins or loses."
(info (level-to-str lvl))
(let [level (ref (add-d lvl))
panel (poor-game-panel level)
action-listener (create-action-listener level panel end-fn)
Expand Down
14 changes: 13 additions & 1 deletion src/snakejure/menu_gui.clj
@@ -1,12 +1,23 @@
(ns snakejure.menu-gui
(:import (javax.swing JFrame JList JOptionPane)
(java.awt.event KeyAdapter KeyEvent)
(java.awt Dimension))
(java.awt Dimension)
(org.apache.log4j Logger FileAppender SimpleLayout Level))
(:use [snakejure.level-loader :only (get-levels-map)]
[snakejure.game-gui :only (create-game-panel)]))

(def width 100)
(def height 400)
(def *log-file* "log.txt")
(def *log-level* (Level/TRACE))

(defn init-logging
"Inits logging to log all level changes to *log-file*"
[]
(doto (Logger/getRootLogger)
(.addAppender (FileAppender. (SimpleLayout.) *log-file* false))
(.setLevel *log-level*)))


(defn- create-poor-jlist []
"Creates Jlist without any listeners."
Expand Down Expand Up @@ -54,6 +65,7 @@
(defn show-menu []
(let [frame (JFrame. "Snakejure")
list (create-jlist frame)]
(init-logging)
(doto frame
(.add list)
(.setDefaultCloseOperation (JFrame/EXIT_ON_CLOSE))
Expand Down

0 comments on commit 7c47549

Please sign in to comment.