1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.LinkedList;
import java.util.Queue;
 
public class LogTailerQueue {
    public static Queue<String> queue = new LinkedList<String>();
    public static String[] alarm = new String[15];
    public static int current;
    private static String lastMsg;
    synchronized public static void put(String str) {        
        /* 중복제거 */
        if(str.equals(lastMsg)) {
            //System.out.println("중복발생:" + lastMsg);
            return;
        }else {
            lastMsg = str;
        }
        queue.add(str);
        String[] split = str.split("#");
        alarm[current] = split[0];
        current++;
        if(current >= 15) current = 0;
    }
    synchronized public static String poll() {
        return queue.poll();
    }
    
    public static int getCurrentErrCnt() {
        int count = 0;
        for(String code : alarm) {            
            if("ERR".equals(code)) count++;
        }
        return count;
    }
 
}
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.LinkedList;
import java.util.Queue;
 
public class LogTailerOPPQueue {
    public static Queue<String> queue = new LinkedList<String>();
    
    synchronized public static void put(String str) {
        queue.add(str);
    }
    synchronized public static String poll() {
        return queue.poll();
    }
 
}
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
public class LogTailerMain {
    public static void main(String[] args) {
        LogTailer logTailer = new LogTailer();
        LogConsolReader logReader = new LogConsolReader();
        LogQueueReader logQueueReader = new LogQueueReader();
        LogERRWriter logErrWriter = new LogERRWriter();
        LogOPPWriter logOppWriter = new LogOPPWriter();
        
        ExecutorService exeService = Executors.newFixedThreadPool(5);
        exeService.execute(logTailer);
        exeService.execute(logReader);
        exeService.execute(logQueueReader);
        exeService.execute(logErrWriter);
        exeService.execute(logOppWriter);
        
        exeService.shutdown();
        
        //exeService.shutdown();
//        
//        LogTailerQueue.put("hello");
//        LogTailerQueue.put("hello");
//        LogTailerQueue.put("hello");
//        LogTailerQueue.put("hello");
//        
//        System.out.println(LogTailerQueue.poll());
//        System.out.println(LogTailerQueue.poll());
//        System.out.println(LogTailerQueue.poll());
//        System.out.println(LogTailerQueue.poll());
//        System.out.println(LogTailerQueue.poll());
        
    }
}
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.LinkedList;
import java.util.Queue;
 
public class LogTailerERRQueue {
    public static Queue<String> queue = new LinkedList<String>();
    
    synchronized public static void put(String str) {
        queue.add(str);
    }
    synchronized public static String poll() {
        return queue.poll();
    }
 
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class LogTailer implements Runnable {
    File file;
    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String readLine;
            while(true) {
                readLine = br.readLine();
                if(readLine == null) {
                    Thread.sleep(10);
                }else {
                    if(readLine.length() != 0) {
                        //System.out.println("[" + readLine + "]");                        
                        LogTailerQueue.put(readLine);
                        if("Q".equals(readLine)) {
                            System.out.println("종료Q");
                            break;
                        }
                    }
                }
            }
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public LogTailer() {
        this.file = new File("./LOG/log.txt");
    }
 
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
 
public class LogQueueReader implements Runnable {
    private static final String ERROR = "ERR";
    private static final String OPPR = "OPP";    
    private File alarmLog;
    private BufferedWriter bw;
    
    public LogQueueReader() {        
        alarmLog = new File("./LOG/ARMLOG.TXT");
        try {
            bw = new BufferedWriter(new FileWriter(alarmLog));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    @Override
    public void run() {
        // TODO Auto-generated method stub
        String lastMsg = "";
        while(true) {
            String readLog = LogTailerQueue.poll();
            if(readLog != null && readLog.length() !=0 ) {    
                
                /* 종료체크 */
                if("Q".equals(readLog)) {
                    LogTailerERRQueue.put("Q");
                    LogTailerOPPQueue.put("Q");
                    System.out.println("종료Q");
                    break;
                }
                
                /* 경고로그 */
                armLogWrite();
                
                String[] array = readLog.split("#");
                if(ERROR.equals(array[0])) {
                    LogTailerERRQueue.put(readLog);
                }else {
                    LogTailerOPPQueue.put(readLog);
                }
                
            }else {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    
    private void armLogWrite() {
        int currentErrCnt = LogTailerQueue.getCurrentErrCnt();
        if(currentErrCnt >= 7 ) {
            try {
                bw.write("ARM#에러" + currentErrCnt);
                bw.newLine();
                bw.flush();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }
 
}
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
 
public class LogOPPWriter implements Runnable {
    public static boolean stopFlag = false;
    private File logFile;    
    public LogOPPWriter() {
        
    }
    
    @Override
    public void run() {
        // TODO Auto-generated method stub
        BufferedWriter bw = null;
        int writeCont = 0;
        int fileCount = 1;
        
        while(!stopFlag) {
            try {
 
                String readLog = LogTailerOPPQueue.poll();
                if(readLog != null && readLog.length() != 0) {
                    System.out.println(readLog);
                    
                    /* 종료체크 */
                    if("Q".equals(readLog)) {    
                        System.out.println("종료Q");
                        break;
                    }
                    
                    if(writeCont % 10 == 0) {
                        logFile = new File("./LOG/OPPLOG_" + fileCount++ + ".TXT");
                        if(bw != null)bw.close();
                        bw = new BufferedWriter(new FileWriter(logFile));
                    }
                    
                    bw.write(readLog);
                    bw.newLine();
                    bw.flush();
                    writeCont++;
                }else {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
 
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
 
    
 
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
 
public class LogGenerator {
    
    static String stAlpa = "ABCDEFGHIJKLMLOPQRSTUVWXYZ";
    static char[] chAlpa = stAlpa.toCharArray();
    
    
    public static void main(String[] args) {        
        File logDir = new File("./LOG");
        if(!logDir.exists()) logDir.mkdirs();
        
        File logFile = new File("./LOG/log.txt");
        
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(logFile, true)));
            while(true) {
                bw.write(genNrmErr() +"#"+ genMessage());
                bw.newLine();                
                bw.flush();                
                Thread.sleep(10);
            }
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    
    public static String genMessage() {
        char[] ranStr = new char[10];
        
        for(int i = 0 ; i < 10 ; i++) {
            int randNum = (int)(Math.random() * 26);
            ranStr[i] = chAlpa[randNum];
        }
        
        return new String(ranStr);
        
    }
    
    public static String genNrmErr() {
        int randNum = (int)(Math.random() * 2);
        if(randNum > 0) {
            return "OPP";
        }else {
            return "ERR";
        }
    }
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
 
public class LogERRWriter implements Runnable {
    public static boolean stopFlag = false;
    private File logFile;    
    public LogERRWriter() {
        
    }
    
    @Override
    public void run() {
        // TODO Auto-generated method stub
        BufferedWriter bw = null;
        int writeCont = 0;
        int fileCount = 1;
        
        while(!stopFlag) {
            try {
                
                String readLog = LogTailerERRQueue.poll();
                if(readLog != null && readLog.length() != 0) {
                    System.out.println(readLog);
                    
                    /* 종료체크 */
                    if("Q".equals(readLog)) {    
                        System.out.println("종료Q");
                        break;
                    }
                    
                    if(writeCont % 10 == 0) {
                        logFile = new File("./LOG/ERRLOG_" + fileCount++ + ".TXT");
                        if(bw != null)bw.close();
                        bw = new BufferedWriter(new FileWriter(logFile));
                    }
                    bw.write(readLog);
                    bw.newLine();
                    bw.flush();
                    writeCont++;
 
                }else {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
 
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
 
    
 
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class LogConsolReader implements Runnable {
 
    @Override
    public void run() {
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String readLine;
        try {
            while((readLine = br.readLine()) != null) {
                LogTailerQueue.put(readLine);
                System.out.println(readLine);
                if("Q".equals(readLine)) break;
                
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 
}
cs


+ Recent posts