/*
* Copyright (C) 2011-2014 GeoForge Project
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.geoforge.java.util.logging.filehandler;
import java.io.File;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.SimpleFormatter;
import org.geoforge.java.awt.dialog.GfrAwtDialogMessageError;
/**
*
* @author bantchao
*
* email: bantchao_AT_gmail.com
* ... please remove "_AT_" from the above string to get the right email address
*
*/
public class FileHandlerLogger extends FileHandler
{
final static private String _STR_BODY_DIALOG_ERROR_ =
"Sorry, an error occured while initializing the application!"
+ "\n ... more in default console"
+ "\n\n The application will now exit";
final static private String _F_S_STR_NAME_FOLDER_PUBLIC_ = "public";
final static private String _F_S_STR_NAME_FOLDER_PROTECTED_ = "protected";
final static private String _F_S_STR_NAME_FOLDER_PRIVATE_ = ".private"; // MEMO: NOT A TYPO ERROR !!!!
final static private String _F_STR_NAME_LOG_PREFIX_ = "runtime";
final static private String _F_STR_SUFFIX_LOG_ = "." + "log";
final static private String _F_STR_NAMELOGJAVA =
FileHandlerLogger._F_STR_NAME_LOG_PREFIX_ + "Java" + FileHandlerLogger._F_STR_SUFFIX_LOG_;
final static private String _F_STR_NAMELOGCPP =
FileHandlerLogger._F_STR_NAME_LOG_PREFIX_ + "Cpp" + FileHandlerLogger._F_STR_SUFFIX_LOG_;
// ---
static private String _STR_PATH_LOG_DIR_PARENT = null;
static private String[] _STRS_LOGS_PARENT_FOLDERS_;
static private String _STR_APPLI_VERSION_TRANSFORMED_;
static private FileHandlerLogger _INSTANCE_ = null;
static public void s_init(String[] strsLogsParentFolder, String strVersionTransformed)
{
try
{
FileHandlerLogger._STRS_LOGS_PARENT_FOLDERS_ = strsLogsParentFolder;
FileHandlerLogger._STR_APPLI_VERSION_TRANSFORMED_ = strVersionTransformed;
File fleFolderVersionApp = _s_getOrCreateFileFolderVersionApplication_();
if (fleFolderVersionApp == null)
{
String strError = "fleFolderVersionApp == null";
strError += "\n" + "FileHandlerLogger.class.getName()=" + FileHandlerLogger.class.getName();
strError += "\n" + "method:" + "s_init(...)";
throw new Exception(strError);
}
FileHandlerLogger._STR_PATH_LOG_DIR_PARENT = fleFolderVersionApp.getAbsolutePath();
// creating public, protected and private directories if ! existing
FileHandlerLogger._s_createFolderChild_(fleFolderVersionApp, FileHandlerLogger._F_S_STR_NAME_FOLDER_PUBLIC_);
FileHandlerLogger._s_createFolderChild_(fleFolderVersionApp, FileHandlerLogger._F_S_STR_NAME_FOLDER_PROTECTED_);
FileHandlerLogger._s_createFolderChild_(fleFolderVersionApp, FileHandlerLogger._F_S_STR_NAME_FOLDER_PRIVATE_);
}
catch (Exception exc)
{
String strErrorMessageConsole = exc.getMessage();
strErrorMessageConsole += "\nExiting...";
System.err.println(strErrorMessageConsole);
GfrAwtDialogMessageError dlg = new GfrAwtDialogMessageError(null,
_STR_BODY_DIALOG_ERROR_);
dlg.setVisible(true);
dlg = null;
System.exit(1);
}
}
static private void _s_createFolderChild_(File fleParent, String strNameChild) throws Exception
{
File fleChild = new File(fleParent, strNameChild);
if (fleChild.exists())
{
if (fleChild.isDirectory())
return;
String strError = "";
strError += "\nfleChild.exists()";
strError += "\n!fleChild.isDirectory()";
strError += "\nfleChild.getAbsolutePath()=" + fleChild.getAbsolutePath();
strError += "\n" + "FileHandlerLogger.class.getName()=" + FileHandlerLogger.class.getName();
strError += "\n" + "method:" + "_s_createFolderChild(...)";
throw new Exception(strError);
}
// ---
// memo: (!fleChild.exists())
if (fleChild.mkdir())
return;
// --- failed
String strError = "";
strError += "\n!fleChild.exists()";
strError += "\n!fleChild.mkdir()";
strError += "\nleChild.getAbsolutePath()=" + fleChild.getAbsolutePath();
strError += "\n" + "FileHandlerLogger.class.getName()=" + FileHandlerLogger.class.getName();
strError += "\n" + "method:" + "_s_createFolderChild(...)";
throw new Exception(strError);
}
static public String s_getPathAbsoluteCachePublicAppli()
{
return FileHandlerLogger._STR_PATH_LOG_DIR_PARENT
+ java.io.File.separator
+ FileHandlerLogger._F_S_STR_NAME_FOLDER_PUBLIC_;
}
static public String s_getPathAbsoluteCacheProtectedAppli()
{
return FileHandlerLogger._STR_PATH_LOG_DIR_PARENT
+ java.io.File.separator
+ FileHandlerLogger._F_S_STR_NAME_FOLDER_PROTECTED_;
}
static public String s_getPathAbsoluteCachePrivateAppli()
{
return FileHandlerLogger._STR_PATH_LOG_DIR_PARENT
+ java.io.File.separator
+ FileHandlerLogger._F_S_STR_NAME_FOLDER_PRIVATE_;
}
static public String s_getPathAbsFileLogJava()
{
return FileHandlerLogger.s_getPathAbsoluteCachePublicAppli()
+ java.io.File.separator
+ FileHandlerLogger._F_STR_NAMELOGJAVA;
}
static public String s_getPathAbsFileLogCpp()
{
return FileHandlerLogger.s_getPathAbsoluteCachePublicAppli()
+ FileHandlerLogger._F_S_STR_NAME_FOLDER_PUBLIC_
+ java.io.File.separator
+ FileHandlerLogger._F_STR_NAMELOGCPP;
}
static public FileHandlerLogger s_getInstance()
{
try
{
if (FileHandlerLogger._INSTANCE_ == null)
{
FileHandlerLogger._INSTANCE_ = new FileHandlerLogger();
}
}
catch (Exception exc)
{
exc.printStackTrace();
String strErrorMessageConsole = exc.getMessage();
strErrorMessageConsole += "\n" + "FileHandlerLogger.class.getName()=" + FileHandlerLogger.class.getName();
strErrorMessageConsole += "\n" + "method:" + "s_getInstance()";
strErrorMessageConsole += "\nExiting...";
System.err.println(strErrorMessageConsole);
GfrAwtDialogMessageError dlg = new GfrAwtDialogMessageError(null,
_STR_BODY_DIALOG_ERROR_);
dlg.setVisible(true);
dlg = null;
System.exit(1);
}
return FileHandlerLogger._INSTANCE_;
}
// trick
private FileHandlerLogger()
throws
IOException,
SecurityException
{
super(FileHandlerLogger.s_getPathAbsFileLogJava());
super.setFormatter(new SimpleFormatter());
}
static private File _s_getOrCreateFileFolderVersionApplication_() throws
Exception
{
File fleParent = new File(System.getProperty("user.home"));
if (fleParent == null)
{
String strError = "fleParent == null";
throw new Exception(strError);
}
if (!fleParent.isDirectory())
{
String strError = "! fleParent.isDirectory(), fleParent.getAbsolutePath()=" + fleParent.getAbsolutePath();
throw new Exception(strError);
}
if (!fleParent.canRead() || !fleParent.canWrite()) // !!!!
{
String strError = "!fleParent.canRead() || !fleParent.canWrite(), fleParent.getAbsolutePath()=" + fleParent.getAbsolutePath();
System.err.println(strError);
fleParent = new File("."); // use install dir instead!
}
File fleCur = null;
if (FileHandlerLogger._STRS_LOGS_PARENT_FOLDERS_ == null)
{
String strError = "FileHandlerLogger._STRS_LOGS_PARENT_FOLDERS_ == null";
throw new Exception(strError);
}
for (int i = 0; i < FileHandlerLogger._STRS_LOGS_PARENT_FOLDERS_.length - 1; i++)
{
fleCur = new File(fleParent, FileHandlerLogger._STRS_LOGS_PARENT_FOLDERS_[i]);
if (fleCur == null)
{
String strError = "fleCur == null";
strError += "\n" + "fleParent.getAbsolutePath()=" + fleParent.getAbsolutePath();
strError += "\n" + "FileHandlerShr.STRS_LOGS_PARENT_FOLDERS[i]=" + FileHandlerLogger._STRS_LOGS_PARENT_FOLDERS_[i];
throw new Exception(strError);
}
if (!fleCur.exists())
{
fleCur.mkdir();
}
else if (!fleCur.isDirectory())
{
String strError = "! fleCur.isDirectory()";
strError += "\n" + "fleCur.getAbsolutePath()=" + fleCur.getAbsolutePath();
throw new Exception(strError);
}
fleParent = fleCur;
}
fleCur = new File(fleParent, FileHandlerLogger._STR_APPLI_VERSION_TRANSFORMED_);
if (!fleCur.exists())
{
fleCur.mkdir();
}
else if (!fleCur.isDirectory())
{
String strError = "! fleCur.isDirectory()";
strError += "\n" + "fleCur.getAbsolutePath()=" + fleCur.getAbsolutePath();
throw new Exception(strError);
}
return fleCur;
}
}