Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 87504
Collapse All | Expand All

(-)EclipseLog.java (+104 lines)
Lines 51-59 Link Here
51
	 */
51
	 */
52
	protected Writer writer;
52
	protected Writer writer;
53
53
54
	public static final int DEFAULT_LOG_SIZE	= 1000;				//$NON-NLS-1$
55
	public static final int MIN_LOG_SIZE		= 10;				//$NON-NLS-1$
56
	public static final int DEFAULT_LOG_FILES	= 1;				//$NON-NLS-1$
57
58
	public static final String LOG_SIZE_MAX		= "eclipse.log.size.max";	//$NON-NLS-1$
59
	public static final String LOG_FILE_MAX		= "eclipse.log.backup.max";	//$NON-NLS-1$
60
	public static final String LOG_EXT			= ".log";					//$NON-NLS-1$
61
	public static final String BACKUP_MARK		= ".bak_";					//$NON-NLS-1$
62
63
	int maxLogSize	= DEFAULT_LOG_SIZE;	// The value is in KB.
64
	int maxLogFiles	= DEFAULT_LOG_FILES;
65
	int backupIdx	= 0;
66
54
	public EclipseLog(File outFile) {
67
	public EclipseLog(File outFile) {
55
		this.outFile = outFile;
68
		this.outFile = outFile;
56
		this.writer = null;
69
		this.writer = null;
70
		readLogProperties();
57
	}
71
	}
58
72
59
	public EclipseLog(Writer writer) {
73
	public EclipseLog(Writer writer) {
Lines 216-221 Link Here
216
		if (logEntry == null)
230
		if (logEntry == null)
217
			return;
231
			return;
218
		try {
232
		try {
233
			checkLogFileSize();
219
			openFile();
234
			openFile();
220
			if (newSession) {
235
			if (newSession) {
221
				writeSession();
236
				writeSession();
Lines 247-252 Link Here
247
	}
262
	}
248
263
249
	public synchronized void setFile(File newFile, boolean append) throws IOException {
264
	public synchronized void setFile(File newFile, boolean append) throws IOException {
265
		if (newFile != null && !newFile.equals(this.outFile)) {
266
			// If it's a new file, then reset.
267
			readLogProperties();
268
			backupIdx = 0;
269
		}
250
		setOutput(newFile, null, append);
270
		setOutput(newFile, null, append);
251
		System.getProperties().put(EclipseStarter.PROP_LOGFILE, newFile.getAbsolutePath());
271
		System.getProperties().put(EclipseStarter.PROP_LOGFILE, newFile.getAbsolutePath());
252
	}
272
	}
Lines 414-417 Link Here
414
	protected void writeSpace() throws IOException {
434
	protected void writeSpace() throws IOException {
415
		write(" "); //$NON-NLS-1$
435
		write(" "); //$NON-NLS-1$
416
	}
436
	}
437
438
	protected boolean checkLogFileSize() {
439
		if (maxLogSize == 0)
440
			return true;	// no size limitation.
441
442
		boolean isBackupOK = true;
443
		if (outFile != null) {
444
			if ((outFile.length()>>10) > maxLogSize) {  // Use KB as file size unit.
445
				String logFilename = outFile.getAbsolutePath();
446
447
				// Delete old backup file that will be replaced.
448
				String replacedFilename = ""; //$NON-NLS-1$
449
				if (logFilename.toLowerCase().endsWith(LOG_EXT)) {
450
					replacedFilename =
451
							logFilename.substring(0, logFilename.length()-LOG_EXT.length())
452
							+ BACKUP_MARK + backupIdx + LOG_EXT;
453
				}
454
				else {
455
					replacedFilename = logFilename + BACKUP_MARK + backupIdx; //$NON-NLS-1$
456
				}
457
				File replacedFile = new File(replacedFilename);
458
				if (replacedFile.exists()) {
459
					if (!replacedFile.delete()) {
460
						System.err.println("Error when trying to delete old log file: " //$NON-NLS-1$
461
								+ replacedFile.getName());
462
						if (replacedFile.renameTo(new File(replacedFile.getAbsolutePath() + System.currentTimeMillis()))) {
463
							System.err.println("So we rename it to filename: " + replacedFile.getName()); //$NON-NLS-1$
464
						}
465
						else {
466
							System.err.println("And we also cannot rename it!"); //$NON-NLS-1$
467
							isBackupOK = false;
468
						}
469
					}
470
				}
471
472
				// Rename current log file to backup one.
473
				boolean isRenameOK = outFile.renameTo(replacedFile);
474
				if (!isRenameOK) {
475
					System.err.println("Error when trying to rename log file to backup one."); //$NON-NLS-1$
476
					isBackupOK = false;
477
				}
478
				File newFile = new File(logFilename);
479
				setOutput(newFile, null, false);
480
481
				// Write a new SESSION header to new log file.
482
				openFile();
483
				try {
484
					writeSession();
485
					writeln();
486
					writeln("This is a continuation of log file " //$NON-NLS-1$
487
							+ replacedFile.getPath());
488
					writeln("Created Time: " + getDate(new Date(System.currentTimeMillis()))); //$NON-NLS-1$
489
					writer.flush();
490
				}
491
				catch (IOException ioe) {
492
					ioe.printStackTrace();
493
				}
494
				closeFile();
495
				backupIdx = (++backupIdx) % maxLogFiles;
496
			}
497
		}
498
		return isBackupOK;
499
	}
500
501
	protected void readLogProperties() {
502
		Object maxLogSize_obj = System.getProperties().get(LOG_SIZE_MAX);
503
		if (maxLogSize_obj != null && maxLogSize_obj instanceof Integer) {
504
			maxLogSize = ((Integer) maxLogSize_obj).intValue();
505
			if (maxLogSize != 0 && maxLogSize < MIN_LOG_SIZE) {
506
				// If the value is '0', then it means no size limitation.
507
				// Also, make sure no inappropriate(too small) assigned value.
508
				maxLogSize = MIN_LOG_SIZE;
509
			}
510
		}
511
512
		Object maxLogFiles_obj = System.getProperties().get(LOG_FILE_MAX);
513
		if (maxLogFiles_obj != null && maxLogFiles_obj instanceof Integer) {
514
			maxLogFiles = ((Integer) maxLogFiles_obj).intValue();
515
			if (maxLogFiles < 1) {
516
				// Make sure no invalid assigned value. (at least >= 1)
517
				maxLogFiles = DEFAULT_LOG_FILES;
518
			}
519
		}
520
	}
417
}
521
}

Return to bug 87504