2011-05-15 19:33:03 +02:00
package com.Acrobot.ChestShop.Logging ;
2011-06-09 22:54:01 +02:00
import com.Acrobot.ChestShop.Config.Config ;
2011-06-11 17:36:55 +02:00
import com.Acrobot.ChestShop.Config.Property ;
2011-05-29 13:25:25 +02:00
import com.Acrobot.ChestShop.DB.Queue ;
import com.Acrobot.ChestShop.DB.Transaction ;
import com.Acrobot.ChestShop.Shop.Shop ;
import org.bukkit.entity.Player ;
import org.bukkit.inventory.ItemStack ;
import java.text.DateFormat ;
import java.text.SimpleDateFormat ;
import java.util.Date ;
2011-07-23 21:00:47 +02:00
import java.util.logging.Level ;
import java.util.logging.Logger ;
2011-05-29 13:25:25 +02:00
2011-05-15 19:33:03 +02:00
/ * *
* @author Acrobot
* /
public class Logging {
2011-07-23 21:00:47 +02:00
private static final DateFormat dateFormat = new SimpleDateFormat ( " yyyy/MM/dd HH:mm:ss " ) ;
private static final Logger logger = Logger . getLogger ( " ChestShop " ) ;
2011-05-29 13:25:25 +02:00
2011-07-23 21:00:47 +02:00
private static String getDateAndTime ( ) {
2011-05-29 13:25:25 +02:00
Date date = new Date ( ) ;
return dateFormat . format ( date ) ;
}
2011-06-09 22:54:01 +02:00
2011-05-29 13:25:25 +02:00
public static void log ( String string ) {
2011-07-23 21:00:47 +02:00
if ( Config . getBoolean ( Property . LOG_TO_CONSOLE ) ) logger . log ( Level . INFO , " [ChestShop] " + string ) ;
if ( Config . getBoolean ( Property . LOG_TO_FILE ) ) FileWriterQueue . addToQueue ( getDateAndTime ( ) + ' ' + string ) ;
2011-05-29 13:25:25 +02:00
}
2011-06-09 22:54:01 +02:00
public static void logTransaction ( boolean isBuying , Shop shop , Player player ) {
2011-05-29 13:25:25 +02:00
log ( player . getName ( ) + ( isBuying ? " bought " : " sold " ) + shop . stockAmount + ' ' + shop . stock . getType ( ) + " for " + ( isBuying ? shop . buyPrice + " from " : shop . sellPrice + " to " ) + shop . owner ) ;
2011-07-23 21:00:47 +02:00
if ( Config . getBoolean ( Property . LOG_TO_DATABASE ) ) logToDatabase ( isBuying , shop , player ) ;
}
private static void logToDatabase ( boolean isBuying , Shop shop , Player player ) {
2011-05-29 13:25:25 +02:00
Transaction transaction = new Transaction ( ) ;
transaction . setAmount ( shop . stockAmount ) ;
transaction . setBuy ( isBuying ) ;
ItemStack stock = shop . stock ;
transaction . setItemDurability ( stock . getDurability ( ) ) ;
transaction . setItemID ( stock . getTypeId ( ) ) ;
transaction . setPrice ( ( isBuying ? shop . buyPrice : shop . sellPrice ) ) ;
2011-06-09 22:54:01 +02:00
transaction . setSec ( System . currentTimeMillis ( ) / 1000 ) ;
2011-05-29 13:25:25 +02:00
transaction . setShopOwner ( shop . owner ) ;
transaction . setShopUser ( player . getName ( ) ) ;
Queue . addToQueue ( transaction ) ;
2011-05-15 19:33:03 +02:00
}
}