52d33ad5528e6e7d5934ce545b05ca4b10b318ec
[synfig.git] / synfig-studio / trunk / synfigstudio-cph-monitor
1 #!/bin/bash
2
3 # Synfig Crash Monitor script
4 # Copyright (c) 2009 Konstantin Dmitriev
5 #       This package is free software; you can redistribute it and/or
6 #       modify it under the terms of the GNU General Public License as
7 #       published by the Free Software Foundation; either version 2 of
8 #       the License, or (at your option) any later version.
9 #
10 #       This package is distributed in the hope that it will be useful,
11 #       but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 #       General Public License for more details.
14
15 set -e
16 trap writelog INT
17
18 init()
19 {
20 echo "Synfig Crash Monitor is a tool to collect statistics about synfig crashes. All information is collected locally in ~/.synfig/cph directory."
21 echo
22 echo `date +%H:%M` "Synfig Crash Monitor started."
23 STARTED=0
24 RUNTIME=0
25 VERSION=''
26 RELEASE=''
27 BRANCH=''
28 CRASH=0
29 [ ! -d ~/.synfig/cph ] && mkdir -p ~/.synfig/cph || true
30 }
31
32 writelog()
33 {
34         if [[ $STARTED != 0 ]]; then
35                 if [[ $CRASH == 0 ]]; then
36                         echo `date +%H:%M` "Synfig exited normally. Session time: $RUNTIME."
37                 else
38                         echo `date +%H:%M` "Crash detected. Version $VERSION.$RELEASE.$BRANCH, session time: $RUNTIME."
39                 fi
40                 if [ -e ~/.synfig/cph/log ]; then
41                         #check if dump needed
42                         CURRENTDATE=`date +%Y-%m-%d`
43                         LOGMODDATE=`stat -c %y ~/.synfig/cph/log`
44                         LOGMODDATE=${LOGMODDATE%% *}
45                         if [[ $LOGMODDATE != $CURRENTDATE ]]; then
46                                 dumpstats
47                         fi
48                 fi
49                 #write log
50                 echo $VERSION/$BRANCH/$RELEASE $RUNTIME $CRASH >> ~/.synfig/cph/log
51                 CRASH=0
52                 RUNTIME=0
53         else
54                 echo
55         fi
56 }
57
58 dumpstats()
59 {
60         echo `date +%H:%M` 'Dumping stats for previous session...'
61         LOGMODDATE=`stat -c %y ~/.synfig/cph/log`
62         LOGMODDATE=${LOGMODDATE%% *}
63         #get versions
64         VERSIONS=''
65         while read LINE; do
66                 FOUND=0
67                 for VER in $VERSIONS; do
68                         if [[ $VER == ${LINE%% *} ]]; then
69                                 FOUND=1
70                                 break
71                         fi
72                 done
73                 [[ $FOUND == 0 ]] && VERSIONS="$VERSIONS ${LINE%% *}"
74         done < ~/.synfig/cph/log
75         echo "   Logged versions: ${VERSIONS}"
76         for VER in $VERSIONS; do
77                 #generating random record ID
78                 ID=$( echo `date` `ps` | md5sum | md5sum )
79                 ID="${ID:2:16}"
80                 #summarizing time and counting crashes
81                 CRASHCOUNT=0
82                 TIMECOUNT=0
83                 while read LINE; do
84                         if [[ ${LINE%% *} == $VER ]]; then
85                                 TIMECOUNT=`expr $TIMECOUNT + $(echo $LINE| cut -f 2 -d ' ')` || true
86                                 CRASHCOUNT=`expr $CRASHCOUNT + $(echo $LINE| cut -f 3 -d ' ')` || true
87                         fi
88                 done < ~/.synfig/cph/log
89                 echo "   $LOGMODDATE $ID $VER $TIMECOUNT $CRASHCOUNT"
90                 echo "$LOGMODDATE $ID $VER $TIMECOUNT $CRASHCOUNT" >> ~/.synfig/cph/stats
91         done
92         rm -f ~/.synfig/cph/log
93         echo '   Done.'
94 }
95
96 mainloop()
97 {
98         while true; do
99                 if ( ps -f -u `whoami`|egrep "synfigstudio$" >/dev/null ) ; then
100                         #synfigstudio process exist
101                         if [[ $STARTED == 0 ]]; then
102                                 STARTED=1
103                                 RUNTIME=0
104                                 #get version
105                                 P=$(ps -f -u `whoami`|egrep "synfigstudio$"| tr -s ' '| cut -d ' ' -f 8)
106                                 echo 
107                                 if [ ! -e $P ]; then
108                                         P=`which $P`
109                                 fi
110                                 P=`dirname $P`
111                                 echo "   Assuming synfig installed in $P."
112                                 VERSION=`$P/synfig --info|head -n 1|cut -d '-' -f 2`
113                                 RELEASE=`$P/synfig --info|egrep "Development version:"|cut -d ' ' -f 3`
114                                 BRANCH=`$P/synfig --info|egrep "Branch:"|cut -d ' ' -f 2`
115                                 if [[ $BRANCH == '(no branch)' ]]; then
116                                         BRANCH=`$P/synfig --info|egrep "Revision ID:"|cut -d ' ' -f 3`
117                                 fi
118                                 echo `date +%H:%M` "Synfig $VERSION.$RELEASE.$BRANCH started."
119                         else
120                                 let RUNTIME=$RUNTIME+1
121                         fi
122                 else
123                         #no synfigstudio process exist
124                         if [[ $STARTED == 1 ]]; then
125                                 #detect crash
126                                 if [ -e ~/.synfig/fifo ]; then
127                                         CRASH=1
128                                 fi
129                                 writelog
130                                 CRASH=0
131                                 STARTED=0
132                         fi
133                 fi
134                 sleep 1
135         done
136 }
137
138 init
139 mainloop